在谷歌地图API v2,如果我想删除所有的地图标记,我可以简单地做:

map.clearOverlays();

我如何做到这一点在谷歌地图API v3?

看看参考API,我不清楚。


当前回答

清空Googlemap

mGoogle_map.clear();

其他回答

谷歌的Demo Gallery有一个关于他们如何做到这一点的演示:

http://code.google.com/apis/maps/documentation/javascript/examples/overlay-remove.html

您可以查看源代码以查看它们如何添加标记。

长话短说,他们把标记保存在一个全局数组中。当清除/删除它们时,它们循环遍历数组,并在给定的标记对象上调用". setmap (null)"。

然而,这个例子展示了一个“技巧”。对于本例,“清除”意味着将它们从映射中删除,但将它们保留在数组中,这允许应用程序快速将它们重新添加到映射中。在某种意义上,这就像“隐藏”它们。

“Delete”也会清除数组。

我不知道为什么,但是,设置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);
    }
});

你是说删除是指隐藏它们还是删除它们?

如果隐藏:

function clearMarkers() {
            setAllMap(null);
        }

如果你想删除它们:

 function deleteMarkers() {
            clearMarkers();
            markers = [];
        }

注意,我使用数组标记来跟踪它们,并手动重置它。

要从地图中删除所有标记,创建如下函数:

1.addMarker(location):这个函数用于在地图上添加标记

2.clearMarkers():该函数从map中移除所有标记,而不是数组中

3.setMapOnAll(map):这个函数用于在数组中添加标记信息

4.deleteMarkers():该函数通过删除对数组中所有标记的引用来删除它们。

// Adds 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 setMapOnAll(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() {
    setMapOnAll(null);
  }

// Deletes all markers in the array by removing references to them.
      function deleteMarkers() {
        clearMarkers();
        markers = [];
      }
for (i in markersArray) {
  markersArray[i].setMap(null);
}

只在IE上工作。


for (var i=0; i<markersArray.length; i++) {
  markersArray[i].setMap(null);
}

工作在chrome, firefox,如…