最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Vue+Openlayer實(shí)現(xiàn)動(dòng)態(tài)加載geojson的方法

 更新時(shí)間:2021年09月01日 17:02:25   作者:~疆  
本文通過實(shí)例代碼給大家介紹基于Vue+Openlayer實(shí)現(xiàn)動(dòng)態(tài)加載geojson的方法,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

加載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)文章

最新評(píng)論

汉源县| 阿图什市| 黄山市| 海原县| 象山县| 昆山市| 威海市| 饶平县| 名山县| 永安市| 包头市| 金川县| 乐安县| 台东县| 迁安市| 五原县| 米易县| 昌图县| 泾阳县| 马鞍山市| 固镇县| 隆德县| 安多县| 桐庐县| 苗栗县| 宕昌县| 永德县| 赤城县| 海丰县| 东阳市| 阳江市| 工布江达县| 福州市| 青川县| 彭阳县| 柳林县| 成武县| 高雄县| 丰台区| 孝昌县| 辛集市|