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

问题描述 投票:0回答: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();
javascript google-maps google-maps-api-3
3个回答
5
投票

如果您不希望标记在更改缩放比例时重新出现,则需要从标记集群中删除标记。

markerCluster.clearMarkers();

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


0
投票

我这样做解决了它:

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

0
投票

我有一个案例,我需要显示一些标记,而一些需要隐藏。

我是这样解决的:

showMarkers = () => {
        this.markerCluster.clearMarkers();

        this.gmarkers.forEach((marker) => {
            if (includedMarkers.has(marker.id)) {
                marker.setMap(this.map);
                this.markerCluster.addMarker(marker);
            } else {
                marker.setMap(null);
            }
        })
    }
© www.soinside.com 2019 - 2024. All rights reserved.