Javascript 如何在不同的时间间隔后隐藏Google maps圆形对象?
假设我在谷歌地图上有两个cricle对象。Javascript 如何在不同的时间间隔后隐藏Google maps圆形对象?,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,假设我在谷歌地图上有两个cricle对象。 我想在创建3秒后从地图中删除每个圆 这是代码。问题是setTimeout仅适用于我创建的最后一个圆。 我怎样才能让它工作 var map; function initialize() { var mapOptions = { center: new google.maps.LatLng(centerLan, centerLon), zoom: 10 }; map = new google.map
我想在创建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
时它都会被覆盖,您只能删除上次调用中创建的圆圈。您在哪里调用setAlarm
?我猜你会叫它三次?@putvande我从一个json文件中读取数据线,然后将数据线发送到setAlarm()函数以创建一个圆圈。圆圈的数量是动态的。但对于这个例子,我只使用了2。因此,圈数=调用setAlarm()的次数。天哪,我不敢相信我犯了那个错误。非常感谢你!