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

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

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

有谁知道这是什么吗?
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();

最佳答案

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

markerCluster.clearMarkers();

(并且将markerCluster变量设置为全局变量,它当前在AJAX回调函数中是本地的)

关于javascript - 缩放后会显示隐藏的标记-API Google Maps V3,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25297604/

10-12 12:45
查看更多