zoukankan      html  css  js  c++  java
  • openlayers行政区划分其他区域遮盖

    1. 山东省遮盖层工具JS

    /**
     * 资源包引入
     */
    import VectorLayer from "ol/layer/Vector";
    import VectorSource from "ol/source/Vector";
    import LinearRing from "ol/geom/LinearRing";
    import Fill from "ol/style/Fill";
    import { Feature } from "ol";
    import { fromExtent } from "ol/geom/Polygon";
    import { Style,  Stroke } from "ol/style";
    
    
    
    /**
     * 私有方法
     */
    function _coverLayer(url,map) {
      request({
        url: url,
        method: "GET",
        dataType: "json",
        success: (res) => {
          var converGeom = _erase(res.data.features[0].geometry);
          var convertFt = new Feature({
            geometry: converGeom,
          });
          var mystyle = new Style({
            fill: new Fill({
              color: "rgba(0,0,0, 0.2)",
            }),
            stroke: new Stroke({
              color: "#BDBDBD",
               2,
            }),
          });
          let converLayer = new VectorLayer({
            source: new VectorSource(),
            style: mystyle,
          });
          converLayer.getSource().addFeature(convertFt);
          map.addLayer(converLayer);
        },
      });
    }
    
    
    function _erase(geom) {
      var extent = [-180, -90, 180, 90];
      var polygonRing = fromExtent(extent);
      var coords = geom.coordinates;
      coords.forEach((coord) => {
        var linearRing = new LinearRing(coord[0]);
        polygonRing.appendLinearRing(linearRing);
      });
      return polygonRing;
    }
    
    
    
    /**
     * 山东省
     */
    export const shandong = {
      init(map) {
        _coverLayer('/static/city/shandongsheng.json', map)
        map.getView().setCenter([116.98360411285401,36.654264296423754])
        map.getView().setZoom(7)
      }
    }
    

    注: request请求是axios 已经做了封装。如果需要封装demo可查看作者的其他文章。

    如果需要其他省市的json数据可去阿里云获取JSON文件

    http://datav.aliyun.com/tools/atlas

    代码使用的结构是不包含子区域的,如果需要进行区划分需要修改_coverLayer方法

  • 相关阅读:
    C++模板编译模型
    C++继承与构造函数、复制控制
    PHP判断用户是手机端?还是浏览器端访问?
    CentOS6.5搭建LNMP
    星级评分--封装成jquery插件
    扩展thinkphp5的redis类方法
    js实现星级评分之方法一
    js原型与继承
    一个基于Tp3.2(thinkphp3.2)的工会管理系统
    实验楼的php比赛题,网页数据提取。
  • 原文地址:https://www.cnblogs.com/xyqbk/p/14070310.html
Copyright © 2011-2022 走看看