Javascript 在 Google Maps Api v3 中删除标记

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

Remove marker in Google Maps Api v3

javascriptgoogle-maps-api-3google-maps-markers

提问by fillibuster

I'm using this function to add a new marker (and polyline) to a map:

我正在使用此函数向地图添加新标记(和折线):

 function addMarker(location) {

    path = poly.getPath();
    path.push(location);
    marker = new google.maps.Marker({
        position: location,
        icon:'location.png',
        title: poly.inKm() + ' km',
        map: map
    });
    markersArray.push(marker);
}

How can I remove the last marker (for implementing undo)?

如何删除最后一个标记(用于实现撤消)?

Best regards ...

此致 ...

回答by rebeliagamer

RemovingOverlays

移除覆盖

markersArray[markersArray.length-1].setMap(null);

... for path:

...对于路径:

path = poly.getPath();
path.pop();

PolylineOptions, MVCArray.

PolylineOptionsMVCArray

回答by trs

Last Marker is at the index of markersArray.length -1so...

最后一个标记位于markersArray.length -1so...

markersArray[markersArray.length-1].setMap(null);

markersArray[markersArray.length-1].setMap(null);