Leaflet.js:仅显示特定国家/地区,隐藏或隐藏其他国家/地区

问题描述 投票:0回答:1

基于:Dim/Hide rest of map around country with leaflet.js

虽然几乎可以接受公认的答案,但its JSFiddle demo仅适用于旧的单个多边形GeoJSON。现代的多多边形GeoJSON允许使用其他形状来弥补海外领土等问题,因此还引入了JSFiddle演示程序不知道如何处理的其他数组:

// transform geojson coordinates into an array of L.LatLng
var coordinates = france.features[0].geometry.coordinates[0];
var latLngs = [];
for (i=0; i<coordinates.length; i++) {
    latLngs.push(new L.LatLng(coordinates[i][1], coordinates[i][0]));
}

L.mask(latLngs).addTo(map);

我意识到这是所有基本的JavaScript和数组101的东西,但是没有人会碰巧知道如何使上述代码与多角形GeoJSON的其他数组一起工作吗?

javascript leaflet geojson
1个回答
0
投票

[我想通过“现代GeoJSON”,您的意思是“ GeoJSON features of type Polygon有外圈,也有内圈”。因此,这实际上是关于“如何将GeoJSON多多边形几何转换为数组(叶Polygon s)的数组(环)(数组中的点)?”。

[请注意,带内环的多边形的概念绝不是“现代”的:它确实出现在(现在已弃用的)LatLng中,这反过来又从OGC的2008 GeoJSON specs中汲取了很多灵感,可追溯到2003年。

我将利用Leaflet已实现的Simple Features。例如:

coordsToLatLngs static method of L.GeoJSON

[必要时请记住对coordsToLatLngs进行完整性检查; L.GeoJSON参数对于var latlngs = L.GeoJSON.coordsToLatLngs(feature.geometry.coordinates, 1); 为0,对于geometry.typelevelsDeep为1,对于LineString为2。

© www.soinside.com 2019 - 2024. All rights reserved.