javascript 从地图中删除多边形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11272552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove Polygon from the map
提问by Asif Ashraf
Requirements: Using the Leaflet js maps api, when the customer clicks on a marker, a Rectangle should be drawn just below the Marker, centering according to the marker. Then clicking on another marker should remove previous rectangles and draw another rectangle below newly clicked marker.
需求:使用Leaflet js maps api,当客户点击一个marker时,在Marker的正下方绘制一个Rectangle,根据marker居中。然后单击另一个标记应删除以前的矩形并在新单击的标记下方绘制另一个矩形。
Problem: I am using the code below to draw a polygon and I can see a rectangle. And it draws the rectangle on a marker. Then by clicking on another marker a new rectangle is being drawn. But the old rectangle also still exists.
问题:我正在使用下面的代码绘制一个多边形,我可以看到一个矩形。它在标记上绘制矩形。然后通过单击另一个标记绘制一个新的矩形。但是旧的矩形也仍然存在。
Question: How should I implement the behavior, so that when clicking on new marker, the old rectangle will be deleted from the map?
问题:我应该如何实现该行为,以便在单击新标记时,旧矩形将从地图中删除?
//polygon
var latBlockSize = 0.002;
var lngBlockSize = 0.002;
var route = [
new L.LatLng(parseFloat(customer.MailingAddress.Lat) + latBlockSize, parseFloat(customer.MailingAddress.Lng) - lngBlockSize),
new L.LatLng(parseFloat(customer.MailingAddress.Lat) + latBlockSize, parseFloat(customer.MailingAddress.Lng) + lngBlockSize),
new L.LatLng(parseFloat(customer.MailingAddress.Lat) - latBlockSize, parseFloat(customer.MailingAddress.Lng) + lngBlockSize),
new L.LatLng(parseFloat(customer.MailingAddress.Lat) - latBlockSize, parseFloat(customer.MailingAddress.Lng) - lngBlockSize)
];
window.polygon = new L.Polygon(route);
window.map.addLayer(window.polygon);
回答by Asif Ashraf
I figured it out by myself.
我自己想出来的。
This was the solution:
这是解决方案:
window.map.removeLayer(window.polygon);
回答by arsenik
That is working as well, tested with Leaflet 1.2.0.
这也有效,已使用 Leaflet 1.2.0 进行测试。
window.polygon.remove()
window.polygon.remove()