在谷歌地图API v2,如果我想删除所有的地图标记,我可以简单地做:
map.clearOverlays();
我如何做到这一点在谷歌地图API v3?
看看参考API,我不清楚。
在谷歌地图API v2,如果我想删除所有的地图标记,我可以简单地做:
map.clearOverlays();
我如何做到这一点在谷歌地图API v3?
看看参考API,我不清楚。
当前回答
下面来自Anon的效果很好,尽管在反复清除覆盖时有闪烁。
简单地做以下几点:
I.声明一个全局变量:
var markersArray = [];
2定义一个函数:
function clearOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
}
}
3在调用以下函数之前,在'markerArray'中插入标记:
markersArray.push(marker);
google.maps.event.addListener(marker,"click",function(){});
IV.在任何需要的地方调用clearoverlay()函数。
就是这样! !
希望这对你有所帮助。
其他回答
我使用的速记法很好地完成了这项工作。只做
map.clear();
在两个答案中发布的“set_map”函数似乎不再在谷歌Maps v3 API中工作。
我想知道发生了什么事
更新:
似乎谷歌改变了他们的API,使“set_map”不是“setMap”。
http://code.google.com/apis/maps/documentation/v3/reference.html
同样的问题。这段代码不再工作了。
我已经纠正了它,这样改变clearMarkers方法:
set_map(空) ---> setMap(空)
google.maps.Map.prototype.clearMarkers = function() {
for(var i=0; i < this.markers.length; i++){
this.markers[i].setMap(null);
}
this.markers = new Array();
};
文档已更新,包括关于主题的详细信息:https://developers.google.com/maps/documentation/javascript/markers#remove
在这里你可以找到一个如何删除标记的例子:
https://developers.google.com/maps/documentation/javascript/examples/marker-remove?hl=es
// Add a marker to the map and push to the array.
function addMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map
});
markers.push(marker);
}
// Sets the map on all markers in the array.
function setAllMap(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
setAllMap(null);
}
// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
clearMarkers();
markers = [];
}
如果你使用gmap V3插件: $(" #地图”).gmap(“removeAllMarkers”);
参见:http://www.smashinglabs.pl/gmap/documentation #后载荷