[英]Cant remove markers from leaflet map. Not using layers
刚刚发现 Leafletjs 并喜欢它。 当我的 json 为空或无效时,我一直试图删除我所有的制造商,但我无法正确处理。 每次我的 json 更新时,我都尝试了闪烁/闪烁的所有不同方法,这是我设法获得的最接近的方法。
任何帮助都会很棒。 我对制造商移动和更新而不眨眼的例子太少了……我真的不想使用谷歌地图!
我试图重置制造商 = {}; 但这没有任何作用。 谢谢
data.BMS.forEach(function (obj) {
if (obj.lat !== undefined && obj.lng !== undefined) {
if (!markers.hasOwnProperty(obj.id)) {
markers[obj.id] = new L.Marker([obj.lat, obj.lng], {icon: panicNormal}).addTo(map) .bindTooltip(obj.name,
{
permanent: true,
direction: 'top',
offset: [0, 0]
});
markers[obj.id].previousLatLngs = [];
areaBounds.push([obj.lat, obj.lng]);
} else {
areaBounds.push([obj.lat, obj.lng]);
markers[obj.id].previousLatLngs.push(markers[obj.id].getLatLng());
if(obj.status == "TRUE"){
markers[obj.id].setIcon(panicAlarm);
}else{
if(obj.type == "MO"){
markers[obj.id].setIcon(panicNormal);
}else{
markers[obj.id].setIcon(lora);
}
}
markers[obj.id].setLatLng([obj.lat, obj.lng]);
}
}else{
//How do I remove the markers
}
});
您可以使用L.FeatureGroup()
向其添加所有标记,然后使用.clearLayers()
删除所有标记
var fg = L.featureGroup().addTo(map);
...
markers[obj.id] = new L.Marker([obj.lat, obj.lng], {icon: panicNormal}).addTo(fg) .bindTooltip
...
}else{
//How do I remove the markers
fg.clearLayers();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.