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]]]]}}]}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值