沿着它周围的圆圈删除标记

时间:2013-02-15 15:28:18

标签: openstreetmap leaflet

我正在使用Open Street Maps并使用leaflet library。 我有一个像这样的图层组:

var testLayer = new L.LayerGroup(); --> declared in a different file

我想在标记组中添加标记,在每个标记周围添加一个圆圈,然后将标记组添加到地图中:

enter image description here

var realMarker = L.marker([52.6432, -6.53412 ],{icon: Icon,title:data[choice][key]["name"]
                        }).bindPopup("Dublin").addTo(testLayer);
var circle = L.circle([52.6432, -6.53412 ],400, {
                        color: 'red',
                        fillColor: '#f03',
                        fillOpacity: 0.5
                    }).addTo(testLayer);

 map.addLayer(testLayer);

但是,当我使用removeLayer()从标记组中删除特定标记时,标记将从地图中删除,但圆圈会留下。如何删除圆圈以及标记?

1 个答案:

答案 0 :(得分:7)

我会在标记中存储对圆圈的引用:

var realMarker = L.marker([52.6432, -6.53412 ],
                          {icon: Icon,title:data[choice][key]["name"]}
                         ).bindPopup("Dublin").addTo(testLayer);
var circle = L.circle([52.6432, -6.53412 ],400, {
                        color: 'red',
                        fillColor: '#f03',
                        fillOpacity: 0.5
                    }).addTo(testLayer);
realMarker.circle=circle;
map.addLayer(testLayer);

然后,在删除标记时,也要删除其圆圈

testLayer.removeLayer(markerToRemove);
testLayer.removeLayer(markerToRemove.circle);