基于Vue+Openlayer實(shí)現(xiàn)動(dòng)態(tài)加載geojson的方法
加載1個(gè)或多個(gè)要素
<template> <div id="map" style="width: 100vw; height: 100vh"></div> </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, ol } from "ol"; import { Style, Stroke, Fill } from "ol/style"; import { Polygon, MultiPolygon } from "ol/geom"; import areaGeo from "@/assets/chengdu.json"; export default { data() { return { map: {}, areaLayer: {}, }; }, mounted() { this.initMap(); //初始化地圖方法 this.addArea(areaGeo); //添加區(qū)域圖層方法 this.pointMove(); this.getFeatureByClick(); }, methods: { pointMove() { // 設(shè)置鼠標(biāo)劃過矢量要素的樣式 this.map.on("pointermove", (e) => { const isHover = this.map.hasFeatureAtPixel(e.pixel); this.map.getTargetElement().style.cursor = isHover ? "pointer" : ""; }); }, getFeatureByClick() { this.map.on("click", (e) => { let features = this.map.getFeaturesAtPixel(e.pixel); this.map.getView().fit(features[0].getGeometry(), { duration: 1500, padding: [100, 100, 100, 100], }); }); }, /** * 設(shè)置區(qū)域 */ addArea(geo = {}) { if (Object.keys(geo).length == 0 && geo.features.length == 0) return; // 設(shè)置圖層 this.areaLayer = new VectorLayer({ source: new VectorSource({ features: [], }), }); // 添加圖層 this.map.addLayer(this.areaLayer); let features = geo.features; for (let i in features) { let areaFeature = {}; if (features[i].geometry.type == "MultiPolygon") { areaFeature = new Feature({ geometry: new MultiPolygon(features[i].geometry.coordinates), }); } else if (features[i].geometry.type == "Polygon") { areaFeature = new Feature({ geometry: new Polygon(features[i].geometry.coordinates), }); } areaFeature.setStyle( new Style({ fill: new Fill({ color: "#4e98f444" }), stroke: new Stroke({ width: 3, color: [71, 137, 227, 1], }), }) ); areaFeature.setProperties(features[i].properties); this.areaLayer.getSource().addFeature(areaFeature); } }, /** * 初始化地圖 */ 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: [103, 31], zoom: 7, }), }); }, }, }; </script>
到此這篇關(guān)于Vue+Openlayer動(dòng)態(tài)加載geojson的文章就介紹到這了,更多相關(guān)Vue Openlayer加載geojson內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue跨域問題:Access?to?XMLHttpRequest?at‘httplocalhost解決
在前端發(fā)出Ajax請(qǐng)求的時(shí)候,有時(shí)候會(huì)產(chǎn)生跨域問題,下面這篇文章主要給大家介紹了關(guān)于vue跨域問題:Access?to?XMLHttpRequest?at‘httplocalhost的解決辦法,需要的朋友可以參考下2023-01-01Vue使用Vue-cropper實(shí)現(xiàn)圖片裁剪
這篇文章主要為大家詳細(xì)介紹了Vue使用Vue-cropper實(shí)現(xiàn)圖片裁剪,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05vue elementui el-form rules動(dòng)態(tài)驗(yàn)證的實(shí)例代碼詳解
在使用elementUI el-form 中,對(duì)于業(yè)務(wù)不同的時(shí)候可能會(huì)產(chǎn)生不同表單結(jié)構(gòu),但是都是存在同一個(gè)表單控件el-form中。這篇文章主要介紹了vue elementui el-form rules動(dòng)態(tài)驗(yàn)證的實(shí)例代碼,需要的朋友可以參考下2019-05-05淺談Vue為什么不能檢測(cè)數(shù)組變動(dòng)
這篇文章主要介紹了淺談Vue為什么不能檢測(cè)數(shù)組變動(dòng),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Vue子組件向父組件通信與父組件調(diào)用子組件中的方法
這篇文章主要介紹了Vue子組件向父組件通信與父組件調(diào)用子組件中的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06vue webpack build資源相對(duì)路徑的問題及解決方法
這篇文章主要介紹了vue webpack build資源相對(duì)路徑的問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Vue中控制v-for循環(huán)次數(shù)的實(shí)現(xiàn)方法
今天小編就為大家分享一篇Vue中控制v-for循環(huán)次數(shù)的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue.js+element-ui動(dòng)態(tài)配置菜單的實(shí)例
今天小編就為大家分享一篇vue.js+element-ui動(dòng)態(tài)配置菜單的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09