简体   繁体   English

无法从传单地图中删除标记。 不使用图层

[英]Cant remove markers from leaflet map. Not using layers

Just discovered Leafletjs and loving it.刚刚发现 Leafletjs 并喜欢它。 I have been trying to remove all my makers when my json is empty or invalid and I just cant get it right.当我的 json 为空或无效时,我一直试图删除我所有的制造商,但我无法正确处理。 All the different approches I have tried blink/flash every time my json updates and this is the closest I have managed to get.每次我的 json 更新时,我都尝试了闪烁/闪烁的所有不同方法,这是我设法获得的最接近的方法。

Any help would be greatfull.任何帮助都会很棒。 I am taken back at how little examples there are of makers moving and updating without blinking.... and I really dont want to use google maps!我对制造商移动和更新而不眨眼的例子太少了……我真的不想使用谷歌地图!

I tried to reset the makers = {};我试图重置制造商 = {}; but this did nothing.但这没有任何作用。 Thank you谢谢


        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
            }
              
        });

You can use L.FeatureGroup() to add all markers to it and then remove all markers with .clearLayers()您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM