如何获取多边形小册子draw
中的所有标记?
创建标记:
for (let i = 0; i < addressPoints.length; i++) {
var point = addressPoints[i];
var marker = L.marker(new L.LatLng(point["lat"], point["lng"]))
.on('click', onClickMarker);
markers.addLayer(marker);
}
并绘制创建的多边形
map.on(L.Draw.Event.CREATED, function (e) {
var layer = e.layer;
drawnItems.addLayer(layer);
var polygon = layer.getLatLngs();
console.log(polygon);
});
我想在polygon
上获得所有标记
答案 0 :(得分:1)
您将要创建一个空数组,并推送所有属于L.Marker
实例且在多边形范围内的元素。
map.on(L.Draw.Event.CREATED, function (e) {
let layer = e.layer;
drawnItems.addLayer(layer);
let polygon = layer.getLatLngs();
console.log(polygon);
let markers = [];
map.eachLayer((layer) => {
if(layer instanceof L.Marker && map.getBounds().contains(polygon)) {
markers.push(layer.feature);
}
});
});
答案 1 :(得分:1)
使您的dranwItems成为一个Leaflet GeoJSON图层组,并在每个Marker上使用Turfjs或Leaflet多边形指向来了解它是否在绘制的图层之一内,如openstreetmap: check if a latlong is outside polygon(region)
中所述