Javascript 使用 OpenLayers,删除标记层和弹出窗口的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4091594/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:34:36  来源:igfitidea点击:

With OpenLayers, what is the correct way of removing a markers layer, and the popups?

javascriptpopupmappingopenlayersmarkers

提问by GilShalit

LoadPin is a function to add a marker to a map. It initializes the layer on the first call. map is an openlayers map object.

LoadPin 是一个向地图添加标记的函数。它在第一次调用时初始化层。map 是一个 openlayers 地图对象。

But using map.removeLayer("markers") or "Markers", does not remove the markers from the map. I saw a mention of a destroy operation to do this but cant find that.

但是使用 map.removeLayer("markers") 或 "Markers",不会从地图中删除标记。我看到提到要执行此操作的销毁操作,但找不到。

AND, how do I remove the popups?

而且,如何删除弹出窗口?

var markers = null
function LoadPin(LL, name, description) {
    var size = new OpenLayers.Size(36, 47);
    var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
    var icon = new OpenLayers.Icon('http://www.waze.co.il/images/home.png', size, offset);

    if (markers == null) {
        markers = new OpenLayers.Layer.Markers("Markers");
        map.addLayer(markers);
    }

    var marker = new OpenLayers.Marker(LL, icon)
    markers.addMarker(marker);
    var bounds = markers.getDataExtent();
    map.zoomToExtent(bounds);

    map.addPopup(new OpenLayers.Popup.FramedCloud("test", LL, null,
                "<div style='font-family:Arial,sans-serif;font-size:0.8em;'>" + name + "<br>" + description + "</div>",
                anchor = null, true, null));
}

回答by Hamish

You can remove individual markers from a marker layer with:

您可以使用以下命令从标记层中删除单个标记:

markers.removeMarker(marker);

Removing the entire layer, with markers should be achieved with:

删除整个图层,应使用标记来实现:

markers.destroy();

You should be able to remove a popup with:

您应该能够删除弹出窗口:

map.removePopup(popup);

where popup is the Popup object created earlier.

其中 popup 是之前创建的 Popup 对象。

回答by Christophe Roussy

I know this post is old but to remove all markers from the marker layer list use:

我知道这篇文章很旧,但要从标记层列表中删除所有标记,请使用:

markerLayer.clearMarkers();

回答by Kau?

Try the any of below code i hope it will help you.

尝试以下任何代码,我希望它会帮助你。

this.markerSource.removeFeature(this.iconFeature); 

or

或者

this.markerSource.removeFeature(iconFeature);