我有以下代码
function init() {
var mapDiv = document.getElementById('map-canvas');
var map = new google.maps.Map(mapDiv, {
center: new google.maps.LatLng(31.949454, 35.932913),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
debugger;
var mapcity = {};
mapcity['amman'] = {
center: new google.maps.LatLng(31.949454, 35.932913),
population: 50000,
name: 'Amman'
};
mapcity['zarqa'] = {
center: new google.maps.LatLng(33.79, 33.39),
population: 100000,
name: 'Zarqa'
};
for (var city in mapcity) {
var cityInfo = {
strokeWeight: 2,
fillColor: 'cyan',
center: mapcity[city].center,
radius: mapcity[city].population,
map: map,
strokeOpacity: 0.8,
fillOpacity: 0.3,
}
Circle = new google.maps.Circle(cityInfo)
google.maps.event.addDomListener(Circle, 'click', function () {
alert(mapcity[city].name);
});
}
};
google.maps.event.addDomListener(window, 'load', init);
我想绘制一个带有两个圆圈和每个圆圈上的事件的地图,但问题是点击事件为两个圆圈提供了相同的值!它给出了两个圈子的第二个城市的城市名称!!!也许是因为它在 for 循环中,所以 Circle 对象将始终采用最后一个值。我尝试使用带有 switch 语句或 IF ELSE 的函数,但它不起作用!!!
请帮忙。