假设我的谷歌地图上有 2 个 cricle 对象。
我想在创建后 3 秒后从 map 上删除每个圆圈。
这是代码。问题是 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 - 如何在不同的时间间隔后隐藏谷歌地图圆圈对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24839714/