3

我查看了大量“类似”问题,但似乎没有一个解决方案/答案对我有用,所以我们开始吧:我正在生成一个带有单个标记“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

4 回答 4

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 );
        }
   };
})();
于 2012-05-17T07:04:51.683 回答
1

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

function clear(){
   if(locations){
        for(var i=0;i<location.length;i++){
            locations[i].setMap(null);
        }
        locations.length=0;
    }
}
于 2012-05-17T07:05:26.957 回答
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);
} 
于 2012-05-17T07:06:58.917 回答
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]
        });
    }
}
于 2012-05-17T07:05:25.490 回答