溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

openlayers4實現(xiàn)點動態(tài)擴(kuò)散的方法

發(fā)布時間:2020-08-19 10:47:02 來源:億速云 閱讀:171 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了openlayers4實現(xiàn)點動態(tài)擴(kuò)散的方法,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

原理:連續(xù)刷新地圖,并且刷新時,修過點樣式的半徑大小,來實現(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]
})


//定義一個矢量圖層,用于打點
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ī)寫的點坐標(biāo)
var points=[]
points.push([112.4,33.5]);
points.push([103.8,23.7]);
points.push([89.7,41.6]);

//將點添加到圖層
points.forEach(element => {
 var ft = new ol.Feature({
  geometry: new ol.geom.Point(element)
 });
 pointAnimationLayer.getSource().addFeature(ft);
});

//map渲染事件,回調(diào)動畫
map.on('postcompose',animation);

//樣式中的半徑變量,通過不斷刷新點樣式的半徑來實現(xiàn)點動態(tài)擴(kuò)散
var radius = 1;

//動畫
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();
}

實現(xiàn)

利用map的渲染事件:postcompose來連續(xù)刷新
之前實現(xiàn)地圖動畫都是用window.setInterval()方法來實現(xiàn),這次拓展下視野,采用Openlayers內(nèi)部的方法。主要有兩步操作:

1、事件注冊

map.on('postcompose',animation);

2、在事件的回調(diào)函數(shù)中去觸發(fā)postcompose事件

map.render();

postcompose事件第一次觸發(fā)是在地圖初始化時,后續(xù)的觸發(fā)都由animation方法中的map.render()來完成。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享openlayers4實現(xiàn)點動態(tài)擴(kuò)散的方法內(nèi)容對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細(xì)的解決方法等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI