Vue+Openlayer批量設(shè)置閃爍點的實現(xiàn)代碼(基于postrender機(jī)制)
效果圖:

實現(xiàn)代碼:
<template>
<div id="map" style="width: 100vw; height: 100vh" />
</template>
<script>
import "ol/ol.css";
import TileLayer from "ol/layer/Tile";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import XYZ from "ol/source/XYZ";
import { Map, View, Feature } from "ol";
import { Style, Circle, Stroke } from "ol/style";
import { Point } from "ol/geom";
import { getVectorContext } from "ol/render";
// 邊界json數(shù)據(jù)
export default {
data() {
return {
map: {},
coordinates: [
{ x: "106.918082", y: "31.441314" }, //重慶
{ x: "86.36158200334317", y: "41.42448570787448" }, //新疆
{ x: "89.71757707811526", y: "31.02619817424643" }, //西藏
{ x: "116.31694544853109", y: "39.868508850821115" }, //北京
{ x: "103.07940932026341", y: "30.438580338450862" }, //成都
],
speed: 0.3,
};
},
mounted() {
this.initMap();
this.addDynamicPoints(this.coordinates);
},
methods: {
/**
* 初始化地圖
*/
initMap() {
this.map = new Map({
target: "map",
layers: [
new TileLayer({
source: new XYZ({
url: "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
}),
}),
],
view: new View({
projection: "EPSG:4326",
center: [108.522097, 37.272848],
zoom: 4.7,
}),
});
},
/**
* 批量添加閃爍點
*/
addDynamicPoints(coordinates) {
// 設(shè)置圖層
let pointLayer = new VectorLayer({ source: new VectorSource() });
// 添加圖層
this.map.addLayer(pointLayer);
// 循環(huán)添加feature
let pointFeature = [];
for (let i = 0; i < coordinates.length; i++) {
// 創(chuàng)建feature,一個feature就是一個點坐標(biāo)信息
const feature = new Feature({
geometry: new Point([coordinates[i].x, coordinates[i].y]),
});
pointFeature.push(feature);
}
//把要素集合添加到圖層
pointLayer.getSource().addFeatures(pointFeature);
// 關(guān)鍵的地方在此:監(jiān)聽postrender事件,在里面重新設(shè)置circle的樣式
let radius = 0;
pointLayer.on("postrender", (e) => {
if (radius >= 20) radius = 0;
let opacity = (20 - radius) * (1 / 20); //不透明度
let pointStyle = new Style({
image: new Circle({
radius: radius,
stroke: new Stroke({
color: "rgba(255,0,0" + opacity + ")",
width: 3 - radius / 10, //設(shè)置寬度
}),
}),
});
// 獲取矢量要素上下文
let vectorContext = getVectorContext(e);
vectorContext.setStyle(pointStyle);
pointFeature.forEach((feature) => {
vectorContext.drawGeometry(feature.getGeometry());
});
radius = radius + this.speed; //調(diào)整閃爍速度
//請求地圖渲染(在下一個動畫幀處)
this.map.render();
});
},
},
};
</script>
到此這篇關(guān)于Vue+Openlayer批量設(shè)置閃爍點的實現(xiàn)代碼(基于postrender機(jī)制)的文章就介紹到這了,更多相關(guān)Vue Openlayer閃爍點內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+ElementUI 關(guān)閉對話框清空驗證,清除form表單的操作
這篇文章主要介紹了vue+ElementUI 關(guān)閉對話框清空驗證,清除form表單的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Vue?Vuex搭建vuex環(huán)境及vuex求和案例分享
這篇文章主要介紹了Vue?Vuex搭建vuex環(huán)境及vuex求和案例分享,?Vue?中實現(xiàn)集中式狀態(tài)管理的一個?Vue?插件,對?vue?應(yīng)用中多個組件的共享狀態(tài)進(jìn)行集中式的管理,下文相關(guān)介紹,需要的朋友可以參考一下2022-04-04
element-ui自定義表格如何給input雙向綁定數(shù)據(jù)
這篇文章主要介紹了element-ui自定義表格如何給input雙向綁定數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
vue省市區(qū)三聯(lián)動下拉選擇組件的實現(xiàn)
本篇文章主要介紹了vue省市區(qū)三聯(lián)動下拉選擇組件的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04
vue 封裝自定義組件之tabal列表編輯單元格組件實例代碼
這篇文章主要介紹了vue 封裝自定義組件tabal列表編輯單元格組件實例代碼,需要的朋友可以參考下2017-09-09
vue3如何解決各場景l(fā)oading過度(避免白屏尷尬!)
在開發(fā)的過程中點擊提交按鈕,或者是一些其它場景總會遇到loading加載,下面這篇文章主要給大家介紹了關(guān)于vue3如何解決各場景l(fā)oading過度的相關(guān)資料,避免白屏尷尬,需要的朋友可以參考下2023-03-03

