Google Maps JavaScript API v3删除标记

时间:2012-05-17 06:56:11

标签: javascript jquery google-maps google-maps-api-3 google-maps-markers

我查看了大量“类似”的问题,并且没有一个解决方案/答案似乎对我有用,所以我们开始:我正在生成一个简单的Google Map,其中包含一个标记“onload”然后我有一个菜单列表有点像这样

<ul>
<li class="toggle" id="beaches">Item</li>
<li class="toggle" id="towns">Item</li>
<ul>

单击时使用这些数组来标记填充地图

jQuery -

$(".toggle").click(function(){
setMarkers(map,$(this).attr('id'));
});

var beaches = [
['Beach 1',38.285827, 20.611038, 4],
['Beach 2', 38.304958,20.604515, 5],
['Beach 3',38.301691,20.597649, 3]
];

var towns = [
['Town 1',38.343003, 20.535679, 4],
['Town 2',38.339334,20.545807, 5]
];

我的人口函数如下所示:

function setMarkers(map, locations) {
for (var i = 0; i < locations.length; i++) {
var newmarker = locations[i];
var myLatLng = new google.maps.LatLng(newmarker[1], newmarker[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: newmarker[0],
zIndex: newmarker[3]
});
}

但我需要/想要做的是在添加新标记之前先清除所有标记。建议,谢谢

4 个答案:

答案 0 :(得分:9)

您需要存储标记,并为要从地图中删除的每个标记调用setMap(null)

您可以尝试这样的事情:

var setMarkers = (function() {
    var oldMarkers = null;
    return function(map, locations){
        //Clearing markers, if they exist
        if(oldMarkers && oldMarkers.length !== 0){
            for(var i = 0; i < oldMarkers.length; ++i){
                oldMarkers[i].setMap(null);
            }
        }
        //Adding new markers
        oldMarkers = [];
        for (var i = 0; i < locations.length; i++) {
            var newmarker = locations[i];
            var myLatLng = new google.maps.LatLng(newmarker[1], newmarker[2]);
            var marker = new google.maps.Marker({
               position: myLatLng,
               map: map,
               title: newmarker[0],
               zIndex: newmarker[3]
            });
            oldMarkers.push( marker );
        }
   };
})();

答案 1 :(得分:1)

也许你可以使用一个空数组包含标记的函数:

function clear(){
   if(locations){
        for(var i=0;i<location.length;i++){
            locations[i].setMap(null);
        }
        locations.length=0;
    }
}

答案 2 :(得分:1)

你需要确保你的标记存储在一个数组中然后删除做一些像

这样的事情
// Sets the map on all markers in the array.
      function removeMarkers {
        for (var i = 0; i < markers.length; i++) {
          markers[i].setMap(null);
        }
        markers.length=0;

      }

所以你的代码现在是

var markers [];

function setMarkers(map, locations) {
// remove all markers first 
removeMarkers();
for (var i = 0; i < locations.length; i++) {
var newmarker = locations[i];

var myLatLng = new google.maps.LatLng(newmarker[1], newmarker[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: newmarker[0],
zIndex: newmarker[3]
});
//add to marker array 
markers.push(marker);
} 

答案 3 :(得分:0)

您可以在重新填充地图之前通过调用setMap(null)来定义全局标记数组并清除它们:

var markers = [];

function setMarkers(map, locations) {
    for (var i = 0; i < locations.length; i++) {
        var newmarker = locations[i];
        var myLatLng = new google.maps.LatLng(newmarker[1], newmarker[2]);

        if (markers[i]) {
            markers[i].setMap(null);
        }

        markers[i] = new google.maps.Marker({
            position: myLatLng,
            map: map,
            title: newmarker[0],
            zIndex: newmarker[3]
        });
    }
}