Vue+Openlayer动态加载geojson

加载1个或多个要素

 

<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); //添加区域图层方法
    this.pointMove();
    this.getFeatureByClick();
  },
  methods: {
    pointMove() {
      // 设置鼠标划过矢量要素的样式
      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],
        });
      });
    },
    /**
     * 设置区域
     */
    addArea(geo = {}) {
      if (Object.keys(geo).length == 0 && geo.features.length == 0) return;

      // 设置图层
      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: "#08305d5b" }),
            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>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js是一个流行的JavaScript框架,用于构建用户界面。OpenLayers是一个开源的JavaScript库,用于在Web上显示交互式地图。结合Vue.js和OpenLayers,可以实现读取和展示GeoJSON数据文件的功能。 首先,你需要在Vue.js项目中安装OpenLayers库。可以通过npm或者yarn来安装OpenLayers依赖: ``` npm install ol ``` 或者 ``` yarn add ol ``` 安装完成后,在Vue组件中引入OpenLayers库: ```javascript import 'ol/ol.css'; import { Map, View } from 'ol'; import GeoJSON from 'ol/format/GeoJSON'; import VectorLayer from 'ol/layer/Vector'; import VectorSource from 'ol/source/Vector'; export default { mounted() { this.loadGeoJSON(); }, methods: { loadGeoJSON() { // 读取GeoJSON文件 fetch('path/to/your/geojson/file.geojson') .then(response => response.json()) .then(data => { // 创建矢量源 const vectorSource = new VectorSource({ features: new GeoJSON().readFeatures(data), }); // 创建矢量图层 const vectorLayer = new VectorLayer({ source: vectorSource, }); // 创建地图 const map = new Map({ target: 'map', // HTML元素的id,用于显示地图 layers: [vectorLayer], view: new View({ center: [0, 0], // 地图中心点的坐标 zoom: 10, // 地图缩放级别 }), }); }); }, }, }; ``` 上述代码中,`loadGeoJSON`方法用于读取GeoJSON文件并创建地图。首先使用`fetch`函数获取GeoJSON文件的数据,然后使用`GeoJSON`格式化器将数据转换为矢量要素。接着,创建矢量源和矢量图层,并将矢量图层添加到地图中。最后,创建地图对象并显示在指定的HTML元素中(通过`target`属性指定)。 请注意,上述代码中的`path/to/your/geojson/file.geojson`需要替换为实际的GeoJSON文件路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值