在谷歌地图API v2,如果我想删除所有的地图标记,我可以简单地做:
map.clearOverlays();
我如何做到这一点在谷歌地图API v3?
看看参考API,我不清楚。
在谷歌地图API v2,如果我想删除所有的地图标记,我可以简单地做:
map.clearOverlays();
我如何做到这一点在谷歌地图API v3?
看看参考API,我不清楚。
当前回答
我不知道为什么,但是,设置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);
}
});
其他回答
在两个答案中发布的“set_map”函数似乎不再在谷歌Maps v3 API中工作。
我想知道发生了什么事
更新:
似乎谷歌改变了他们的API,使“set_map”不是“setMap”。
http://code.google.com/apis/maps/documentation/v3/reference.html
在这里你可以找到一个如何删除标记的例子:
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 = [];
}
简单地做以下几点:
I.声明一个全局变量:
var markersArray = [];
2定义一个函数:
function clearOverlays() {
for (var i = 0; i < markersArray.length; i++ ) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
OR
google.maps.Map.prototype.clearOverlays = function() {
for (var i = 0; i < markersArray.length; i++ ) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
3在调用以下函数之前,在'markerArray'中插入标记:
markersArray.push(marker);
google.maps.event.addListener(marker,"click",function(){});
IV.调用clearoverlay ();或map.clearOverlays ();在任何需要的地方发挥作用。
就是这样! !
这是谷歌自己在至少一个样本中使用的方法:
var markers = [];
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
检查谷歌样本完整的代码示例:
https://developers.google.com/maps/documentation/javascript/examples/places-searchbox
使用这个你可以删除地图上的所有标记。
map.clear();
这对你有帮助,对我也有帮助。