Leaflet中的父多边形js

时间:2016-06-06 11:37:48

标签: javascript leaflet openstreetmap

我想在父多边形中包装多个多边形。示例如下:

enter image description here

这在Leaflet js中是否可行?假设我有一组L.polygon个对象。

谢谢

2 个答案:

答案 0 :(得分:5)

简答:不。

如果你想为你的多边形创建一个包络,那么这是一个超出leafletjs范围的算法问题。

您可以查看此question的答案,以便开始解决您的问题。

编辑:这是使用Turfjs库的example(感谢@IvanSanchez的头部和@HudsonPH的多边形)。

// draw envelope
var points = {
  "type": "FeatureCollection",
  "features":
[
 // collect the points of your polygons
 turf.point([-104.05, 48.99]),
 // ...
]
};

var hull = turf.convex(points);
L.geoJson(hull).addTo(map);

答案 1 :(得分:1)

您可以拥有一个组,但您需要定义所有坐标 更多信息:http://leafletjs.com/examples/geojson.html

    var states = [{
    "type": "Feature",
    "properties": {"party": "Republican"},
    "geometry": {
        "type": "Polygon",
        "coordinates": [[
            [-104.05, 48.99],
            [-97.22,  48.98],
            [-96.58,  45.94],
            [-104.03, 45.94],
            [-104.05, 48.99]
        ]]
    }
}, {
    "type": "Feature",
    "properties": {"party": "Democrat"},
    "geometry": {
        "type": "Polygon",
        "coordinates": [[
            [-109.05, 41.00],
            [-102.06, 40.99],
            [-102.03, 36.99],
            [-109.04, 36.99],
            [-109.05, 41.00]
        ]]
    }
}];

L.geoJson(states, {
    style: function(feature) {
        switch (feature.properties.party) {
            case 'Republican': return {color: "#ff0000"};
            case 'Democrat':   return {color: "#0000ff"};
        }
    }
}).addTo(map);