可以说我在Google地图上有2个cricle对象。
我想在创建3秒后从地图上删除每个圆圈。

这是代码。问题是setTimeout仅适用于我创建的最后一个圆。
我该如何运作?

var map;

function initialize() {
    var mapOptions = {
        center: new google.maps.LatLng(centerLan, centerLon),
        zoom: 10
    };
    map = new google.maps.Map(document.getElementById("map"), mapOptions);
}

google.maps.event.addDomListener(window, 'load', initialize);

function setAlarm(lon, lat) {
    var alarmPosition = {
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 0,
        fillColor: '#FF0000',
        fillOpacity: 0.35,
        map: map,
        center: new google.maps.LatLng(lon, lat),
        radius: Math.sqrt(150) * 100
    };
    // Add the circle for this city to the map.
    cityCircle = new google.maps.Circle(alarmPosition);

    //trying to remove the circle after some time

        setTimeout(function() {
    cityCircle.setMap(null);
    }, 3000);
}

最佳答案

将变量设为本地:

//note the var-keyword
var cityCircle = new google.maps.Circle(alarmPosition);


否则,它将在每次调用setAlarm时覆盖,并且您只能删除在上次调用中创建的圆。

关于javascript - 如何在不同的时间间隔后隐藏Google map 圆对象?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24839714/

10-12 00:30
查看更多