在谷歌地图API v2,如果我想删除所有的地图标记,我可以简单地做:
map.clearOverlays();
我如何做到这一点在谷歌地图API v3?
看看参考API,我不清楚。
在谷歌地图API v2,如果我想删除所有的地图标记,我可以简单地做:
map.clearOverlays();
我如何做到这一点在谷歌地图API v3?
看看参考API,我不清楚。
当前回答
在这里你可以找到一个如何删除标记的例子:
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 = [];
}
其他回答
我发现使用google-maps-utility-library-v3项目中的markermanager库是最简单的方法。
1. 设置标记管理器
mgr = new MarkerManager(map);
google.maps.event.addListener(mgr, 'loaded', function () {
loadMarkers();
});
2. 向MarkerManager添加标记
function loadMarkers() {
var marker = new google.maps.Marker({
title: title,
position: latlng,
icon: icon
});
mgr.addMarker(marker);
mgr.refresh();
}
3.要清除标记,只需要调用MarkerManger的clearMarkers()函数
mgr.clearMarkers();
大多数投票的答案是正确的,但如果如果你只有一个标记(就像我曾在我的情况下),每次你需要杀死前面位置的标记,并添加一个新的然后你不需要创建整个数组的标记和管理每个推动和流行,您可以简单地创建一个变量来存储你的标记之前的位置,可以设置为null的创造新的。
//保存标记位置的全局变量。
var previousMarker;
//当添加新的标记时
if(previousMarker != null)
previousMarker.setMap(null);
var marker = new google.maps.Marker({map: resultsMap, position: new google.maps.LatLng(lat_, lang_)});
previousMarker = marker;
我尝试了所有提出的解决方案,但当我所有的标记都在一个集群下时,没有一个对我有效。 最后我写了这个:
var markerCluster = new MarkerClusterer(map, markers,
{ imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m' });
agentsGpsData[agentGpsData.ID].CLUSTER = markerCluster;
//this did the trick
agentsGpsData[agentId].CLUSTER.clearMarkers();
换句话说,如果你在一个集群中包装标记,并想要删除所有标记,你调用:
clearMarkers();
我不知道为什么,但是,设置setMap(null)到我的标记并不适用于我,当我使用DirectionsRenderer。
在我的情况下,我必须调用setMap(null)到我的DirectionsRenderer以及。
就像这样:
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
if (map.directionsDisplay) {
map.directionsDisplay.setMap(null);
}
map.directionsDisplay = directionsDisplay;
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsDisplay.setMap(map);
directionsService.route(request, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
下面来自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()函数。
就是这样! !
希望这对你有所帮助。