问题描述:

I have markers that are stored in an object array and I cannot figure out how to delete them.

// Initialize Object Array

var Calls = [{

lat: 42,

lng: -72

}, {

lat: 40.7127837,

lng: -74.0059413

}, {

lat: 40.735657,

lng: -74.1723667

}];

// Initialize Map

function initMap()

{

map = new google.maps.Map(document.getElementById('map'),

{

center: Calls[0],

zoom: 14,

scaleControl: true

})

}

// Add Markers

function initMarkers()

{

for (var i = 0; i < Calls.length; i++)

{

var marker = new google.maps.Marker({

position: Calls[i],

map: map

});

}

}

// Start on Load

window.onload = function()

{

initMap();

initMarkers();

}

//Clear Markers

function clearOverlays()

{

initMarkers(null);

}

// Run Clear Markers Function every 3 seconds

setInterval(function()

{

clearOverlays();

Calls = [];

}, 3000)

网友答案:

Working fiddle.

You have to store markers in other array like :

var markers = [];

for (var i = 0; i < Calls.length; i++)
{
    var marker = new google.maps.Marker({
        position: Calls[i],
        map: map
    });
    markers.push(marker);
}    

After that add function that clear map from all markers :

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

Hope this helps.

相关阅读:
Top