缩放后出现隐藏标记 - API Google Maps V3

Hei*_*ira 2 javascript google-maps google-maps-api-3

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

function clearMarkers() 
{
    for (i in markers) 
    {
       markers[i].setMap(null);
    }
    markers = [];
}
Run Code Online (Sandbox Code Playgroud)

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

有谁知道是什么?

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();
Run Code Online (Sandbox Code Playgroud)

geo*_*zip 5

如果您不希望在更改缩放比例时返回标记,则需要从 markerClusterer 中删除标记。

markerCluster.clearMarkers();
Run Code Online (Sandbox Code Playgroud)

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