在谷歌地图API v2,如果我想删除所有的地图标记,我可以简单地做:
map.clearOverlays();
我如何做到这一点在谷歌地图API v3?
看看参考API,我不清楚。
在谷歌地图API v2,如果我想删除所有的地图标记,我可以简单地做:
map.clearOverlays();
我如何做到这一点在谷歌地图API v3?
看看参考API,我不清楚。
当前回答
清空Googlemap
mGoogle_map.clear();
其他回答
for (i in markersArray) {
markersArray[i].setMap(null);
}
只在IE上工作。
for (var i=0; i<markersArray.length; i++) {
markersArray[i].setMap(null);
}
工作在chrome, firefox,如…
如果你使用gmap V3插件: $(" #地图”).gmap(“removeAllMarkers”);
参见:http://www.smashinglabs.pl/gmap/documentation #后载荷
在这里你可以找到一个如何删除标记的例子:
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 = [];
}
下面来自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()函数。
就是这样! !
希望这对你有所帮助。
google.maps.Map.prototype.markers = new Array();
google.maps.Map.prototype.addMarker = function(marker) {
this.markers[this.markers.length] = marker;
};
google.maps.Map.prototype.getMarkers = function() {
return this.markers
};
google.maps.Map.prototype.clearMarkers = function() {
for(var i=0; i<this.markers.length; i++){
this.markers[i].setMap(null);
}
this.markers = new Array();
};
我认为V3中没有,所以我使用了上面的自定义实现。
免责声明:我没有写这段代码,但我忘记保留引用时,我合并到我的代码库,所以我不知道它来自哪里。