openlayers4實(shí)現(xiàn)點(diǎn)動(dòng)態(tài)擴(kuò)散
本文實(shí)例為大家分享了openlayers4實(shí)現(xiàn)的點(diǎn)動(dòng)態(tài)擴(kuò)散的具體代碼,供大家參考,具體內(nèi)容如下
原理:連續(xù)刷新地圖,并且刷新時(shí),修過點(diǎn)樣式的半徑大小,來實(shí)現(xiàn)擴(kuò)散效果;
//定義底圖
var baseLayer = new ol.layer.Vector({
renderMode: 'image',
source: new ol.source.Vector({
url:'/data/shengjie.geojson',
format: new ol.format.GeoJSON()
}),
style: new ol.style.Style({
fill: new ol.style.Fill({
color: '#0A122A'
}),
stroke: new ol.style.Stroke({
color: '#6E6E6E',
width: 1
})
})
})
var view = new ol.View({
center: [108.7,34.8],
zoom: 4,
projection: "EPSG:4326"
});
var map = new ol.Map({
target: 'map',
view: view,
layers: [baseLayer]
})
//定義一個(gè)矢量圖層,用于打點(diǎn)
var pointAnimationLayer = new ol.layer.Vector({
source: new ol.source.Vector(),
style: new ol.style.Style({
image: new ol.style.Circle({
fill: new ol.style.Fill({
color: '#E6E6E6'
}),
radius: 4
})
})
})
map.addLayer(pointAnimationLayer);
//隨機(jī)寫的點(diǎn)坐標(biāo)
var points=[]
points.push([112.4,33.5]);
points.push([103.8,23.7]);
points.push([89.7,41.6]);
//將點(diǎn)添加到圖層
points.forEach(element => {
var ft = new ol.Feature({
geometry: new ol.geom.Point(element)
});
pointAnimationLayer.getSource().addFeature(ft);
});
//map渲染事件,回調(diào)動(dòng)畫
map.on('postcompose',animation);
//樣式中的半徑變量,通過不斷刷新點(diǎn)樣式的半徑來實(shí)現(xiàn)點(diǎn)動(dòng)態(tài)擴(kuò)散
var radius = 1;
//動(dòng)畫
function animation(event){
if(radius >= 20){
radius = 0
}
var opacity = (20 - radius) * (1 / 20) ;//不透明度
var pointStyle = new ol.style.Style({
image: new ol.style.Circle({
radius:radius,
stroke: new ol.style.Stroke({
color: 'rgba(255,255,0' + opacity + ')',
width: 2 - radius / 10
})
})
})
var features = pointAnimationLayer.getSource().getFeatures();
var vectorContext = event.vectorContext;
vectorContext.setStyle(pointStyle);
features.forEach(element => {
var geom = element.getGeometry();
vectorContext.drawGeometry(geom);
});
radius = radius + 0.3;
//觸發(fā)map的postcompose事件
map.render();
}
實(shí)現(xiàn)
利用map的渲染事件:postcompose來連續(xù)刷新
之前實(shí)現(xiàn)地圖動(dòng)畫都是用window.setInterval()方法來實(shí)現(xiàn),這次拓展下視野,采用Openlayers內(nèi)部的方法。主要有兩步操作:
1、事件注冊(cè)
map.on('postcompose',animation);
2、在事件的回調(diào)函數(shù)中去觸發(fā)postcompose事件
map.render();
postcompose事件第一次觸發(fā)是在地圖初始化時(shí),后續(xù)的觸發(fā)都由animation方法中的map.render()來完成。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js超時(shí)調(diào)用setTimeout和間歇調(diào)用setInterval實(shí)例分析
這篇文章主要介紹了js超時(shí)調(diào)用setTimeout和間歇調(diào)用setInterval,以實(shí)例形式對(duì)比分析了setTimeout與setInterval的具體使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-01-01
純JavaScript基于notie.js插件實(shí)現(xiàn)消息提示特效
這篇文章主要介紹了純JavaScript基于notie.js插件實(shí)現(xiàn)消息提示特效,可以制作Alert提示框,確認(rèn)框和帶輸入的消息框,感興趣的小伙伴們可以參考一下2016-01-01
ES6學(xué)習(xí)筆記之let與const用法實(shí)例分析
這篇文章主要介紹了ES6學(xué)習(xí)筆記之let與const用法,結(jié)合實(shí)例形式分析了ES6中l(wèi)et與const的功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-01-01
JavaScript獲取flash對(duì)象與網(wǎng)上的有所不同
關(guān)于js獲取flash對(duì)象,網(wǎng)上有非常多的例子,但不是我想要的,經(jīng)測(cè)試整理,因此本文誕生了2014-04-04
JavaScript設(shè)計(jì)模式之工廠模式和抽象工廠模式定義與用法分析
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之工廠模式和抽象工廠模式,結(jié)合實(shí)例形式分析了工廠模式的功能、定義、相關(guān)問題解決方法,并分析抽象工廠模式與工廠模式的不同之處,需要的朋友可以參考下2018-07-07
如何在JavaScript中使用localStorage詳情
這篇文章主要介紹了如何在JavaScript中使用localStorage,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
js 動(dòng)態(tài)加載事件的幾種方法總結(jié)
本篇文章主要是對(duì)js 動(dòng)態(tài)加載事件的幾種方法進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-12-12

