缩放后显示隐藏的标记 - API Google Maps V3

时间:2014-08-13 23:03:26

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

我在地图中有标记,我用它来删除它们。

function clearMarkers() 
{
    for (i in markers) 
    {
       markers[i].setMap(null);
    }
    markers = [];
}

但是当我改变地图的缩放时,所有标记都会再次出现。

有谁知道它是什么?

var map;
var idInfoBoxOpen;
var infoBox = [];
var markers = [];

function initialize() 
{   
    var latlng = new google.maps.LatLng(-18.8800397, -47.05878999999999);   
    var options = {
        zoom: 5,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("mapa"), options);
}
initialize();

function openInfoBox(id, marker) {
    if (typeof(idInfoBoxOpen) == 'number' && typeof(infoBox[idInfoBoxOpen]) == 'object') 
    {
        infoBox[idInfoBoxOpen].close();
    }
    infoBox[id].open(map, marker);
    idInfoBoxOpen = id;
}

function loadPoints()
{
    $.getJSON('assets/json/pontos.json', function(points) 
    {
        var latlngbounds = new google.maps.LatLngBounds();
        $.each(points, function(index, point) 
        {
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(point.Latitude, point.Longitude),
                title: "Desc",
                icon: 'assets/img/point.png',
            });

            var myOptions = {
                content: "<p>" + point.Descricao + "</p>",
                pixelOffset: new google.maps.Size(-150, 0)
            };

            infoBox[point.Id] = new InfoBox(myOptions);
            infoBox[point.Id].marker = marker;
            infoBox[point.Id].listener = google.maps.event.addListener(marker, 'click', function (e) {
                openInfoBox(point.Id, marker);
        });
            markers.push(marker);
            latlngbounds.extend(marker.position);   
        });
        var markerCluster = new MarkerClusterer(map, markers);
        map.fitBounds(latlngbounds);

    });

}

function clearMarkers() 
{
    for (i in markers) 
    {
       markers[i].setMap(null);
    }
    markers = [];
}

function reset(value)
{
    clearMarkers();
}

loadPoints();

2 个答案:

答案 0 :(得分:4)

如果您不想让它们在您更改缩放时返回,则需要从markerClusterer中删除标记。

markerCluster.clearMarkers();

(并使markerCluster变量成为全局变量,它当前是AJAX回调函数的本地变量)

答案 1 :(得分:0)

我解决了这个问题:

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