openlayers4實現(xiàn)點動態(tài)擴散
本文實例為大家分享了openlayers4實現(xiàn)的點動態(tài)擴散的具體代碼,供大家參考,具體內(nèi)容如下
原理:連續(xù)刷新地圖,并且刷新時,修過點樣式的半徑大小,來實現(xiàn)擴散效果;
//定義底圖 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); //隨機寫的點坐標 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)擴散 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()來完成。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js超時調(diào)用setTimeout和間歇調(diào)用setInterval實例分析
這篇文章主要介紹了js超時調(diào)用setTimeout和間歇調(diào)用setInterval,以實例形式對比分析了setTimeout與setInterval的具體使用技巧,非常具有實用價值,需要的朋友可以參考下2015-01-01純JavaScript基于notie.js插件實現(xiàn)消息提示特效
這篇文章主要介紹了純JavaScript基于notie.js插件實現(xiàn)消息提示特效,可以制作Alert提示框,確認框和帶輸入的消息框,感興趣的小伙伴們可以參考一下2016-01-01JavaScript獲取flash對象與網(wǎng)上的有所不同
關(guān)于js獲取flash對象,網(wǎng)上有非常多的例子,但不是我想要的,經(jīng)測試整理,因此本文誕生了2014-04-04JavaScript設(shè)計模式之工廠模式和抽象工廠模式定義與用法分析
這篇文章主要介紹了JavaScript設(shè)計模式之工廠模式和抽象工廠模式,結(jié)合實例形式分析了工廠模式的功能、定義、相關(guān)問題解決方法,并分析抽象工廠模式與工廠模式的不同之處,需要的朋友可以參考下2018-07-07如何在JavaScript中使用localStorage詳情
這篇文章主要介紹了如何在JavaScript中使用localStorage,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02