openlayers 地图加载geojson绘制边界,点,线,面

openlayers 地图加载geojson绘制边界,点,线,面

  • 方法
 // 绘制点 / 线 / 面
    drawContent(geo) {
      if (this.routeLayer) {
        this.map.removeLayer(this.routeLayer);
      }

      let features = [];
      geo.features.forEach((g) => {
        let lineData = g.geometry;
        let routeFeature = "";
        if (lineData.type === "MultiPolygon") {
          routeFeature = new Feature({
            geometry: new MultiPolygon(lineData.coordinates),
          });
          routeFeature.setStyle(
            new Style({
              fill: new Fill({
                color: "rgba(0,0,255, 0.5)", //填充颜色
              }),
              stroke: new Stroke({
                width: 2, //边界宽度
                color: [255, 0, 0, 1], //边界颜色
              }),
            })
          );
        } else if (lineData.type === "Polygon") {
          routeFeature = new Feature({
            geometry: new Polygon(lineData.coordinates),
          });
          routeFeature.setStyle(
            new Style({
              fill: new Fill({
                color: "rgba(0,0,255, 0.5)", //填充颜色
              }),
              stroke: new Stroke({
                width: 2, //边界宽度
                color: [255, 0, 0, 1], //边界颜色
              }),
            })
          );
        } else if (lineData.type === "MultiLineString") {
          routeFeature = new Feature({
            geometry: new MultiLineString(lineData.coordinates),
          });
          routeFeature.setStyle(
            new Style({
              fill: new Fill({
                color: "rgba(0,0,255, 1)", //填充颜色
              }),
              stroke: new Stroke({
                width: 2, //边界宽度
                color: [255, 0, 0, 1], //边界颜色
              }),
            })
          );
        } else if (lineData.type === "LineString") {
          routeFeature = new Feature({
            geometry: new LineString(lineData.coordinates),
          });
          routeFeature.setStyle(
            new Style({
              fill: new Fill({
                color: "rgba(0,0,255, 1)", //填充颜色
              }),
              stroke: new Stroke({
                width: 2, //边界宽度
                color: [255, 0, 0, 1], //边界颜色
              }),
            })
          );
        }

        features.push(routeFeature);

        const center = this.getLast(lineData.coordinates);
        this.map.getView().setCenter(center);
      });
      // 设置图层
      this.routeLayer = new VectorLayer({
        source: new VectorSource({
          features: features,
        }),
      });
      // 添加图层
      this.map.addLayer(this.routeLayer);
    },
    // 获取到第一个坐标点,设置成地图中心
    getLast(list) {
      const isArray = list[0] instanceof Array;
      if (!isArray) {
        return list;
      } else {
        return this.getLast(list[0]);
      }
    },
  • geojson示例数据
    获取中国行政区geojson: http://datav.aliyun.com/portal/school/atlas/area_selector
{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"adcode":370321,"name":"xx县","center":[118.101556,36.959773],"centroid":[118.026774,36.992528],"childrenNum":0,"level":"district","acroutes":[100000,370000,370300],"parent":{"adcode":370300}},"geometry":{"type":"MultiPolygon","coordinates":[[[[117.940613,36.891605],[117.940224,36.899505],[117.941006,36.899192],[117.940613,36.891605]]]]}}]}

你可能感兴趣的:(Web前端,vue.js,前端,地图,天地图,ol)