ol基于4326编码的地图遮罩与反向遮罩

前期准备

ol版本7.1.0   node版本16.14.2   npm版本8.5.0

import 'ol/ol.css';
import { Overlay, sphere } from 'ol';
// import { getDistance } from 'ol/sphere';
import Map from 'ol/Map';
import XYZ from 'ol/source/XYZ'
import View from 'ol/View';
import { get, fromLonLat, transform } from 'ol/proj';
import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer';
import { getTopLeft, getWidth } from 'ol/extent';
import Feature from 'ol/Feature';
import { Fill, Stroke, Icon, Style } from 'ol/style'
import { Polygon, MultiPolygon, LinearRing, Point, Circle } from "ol/geom";//添加图层按钮
import VectorSource from 'ol/source/Vector';
import GeoJSON from 'ol/format/GeoJSON.js';
import { fromExtent } from "ol/geom/Polygon";
// import OLCesium from 'olcs';
import geoJson from '../../../public/json/hf.json'


import markerImg from '@/assets/img/bg.png'

正常遮罩

addArea() {
            if (!this.josndata) {
                let geo = [geoJson]
                if (geo.length == 0) {
                    return false;
                }
                let features = [];
                geo.forEach((g) => {
                    console.log(g);

                    let lineData = g.features[0];
                    let routeFeature = "";
                    if (lineData.geometry.type == "MultiPolygon") {
                        routeFeature = new Feature({
                            geometry: new MultiPolygon(lineData.geometry.coordinates),
                        });
                    } else if (lineData.geometry.type == "Polygon") {
                        routeFeature = new Feature({
                            geometry: new Polygon(lineData.geometry.coordinates),
                        });
                    }
                    routeFeature.setStyle(
                        new Style({
                            fill: new Fill({
                                color: "#4e98f444", //填充颜色
                            }),
                            stroke: new Stroke({
                                width: 3, //边界宽度
                                color: [71, 137, 227, 1], //边界颜色
                            }),
                        })
                    );
                    features.push(routeFeature);
                });
                // 设置图层
                this.josndata = new VectorLayer({
                    source: new VectorSource({
                        features: features,
                    }),
                });
                // 添加图层
                this.map.addLayer(this.josndata);
            }

        },

反向遮罩

        //创建蒙层,凸显json区域
        showGuangxiArea() {
            let initLayer = new VectorLayer({
                zIndex: 3,
                source: new VectorSource(),
                style: new Style({
                    fill: new Fill({
                        color: "rgba( 7, 16, 28, 1)",
                    }),
                    stroke: new Stroke({
                        color: "#26d4fa",
                        width: 2
                    })
                })
            });
            this.map.addLayer(initLayer);
            this.addConver(initLayer, geoJson);
        },
        //添加遮罩
        addConver(converLayer, data) {
            const fts = new GeoJSON().readFeatures(data);
            const ft = fts[0];
            const converGeom = this.erase(ft.getGeometry());
            const convertFt = new Feature({
                geometry: converGeom,
            });
            converLayer.getSource().addFeature(convertFt);
        },
        //擦除操作,生产遮罩范围
        erase(geom) {
            const extent = [-180, -90, 180, 90];
            const polygonRing = fromExtent(extent);
            const coords = geom.getCoordinates();
            coords.forEach(coord => {
                const linearRing = new LinearRing(coord[0]);
                polygonRing.appendLinearRing(linearRing);
            });
            return polygonRing;
        },

其中JSON数据是从DataV.GeoAtlas地理小工具系列 (aliyun.com)来拉取的

如果你使用的是3857编码来初始化,请参考下一篇的写法来创建

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值