我在向多个圆形叠加层中的每一个添加信息窗口时遇到问题。 当我点击一个圆圈时,会出现一个信息窗口,但不会出现在我点击的圆圈上,而是放在最后一个圆圈上。似乎每次循环执行时,事件监听器都会添加到每个循环中,但信息窗口的内容是从添加的最后一个循环中派生出来的。
for (i in cityPoints) {
var magnitudeOptions = {
map: map,
center: cityPoints[i].center,
radius: cityPoints[i].magnitude,
id:cityPoints[i].id,
addr:cityPoints[i].addr,
infoWindowIndex: i
};
cityCircle = new google.maps.Circle(magnitudeOptions);
circlesArray.push(cityCircle);
infoWindow = new google.maps.InfoWindow({ content: cityPoints[i].id + " " + cityPoints[i].addr });
infoWindowsArray.push(infoWindow);
google.maps.event.addListener(circlesArray[i], 'click', function (ev) {
infoWindowsArray[i].setPosition(cityPoints[i].center);
infoWindowsArray[i].open(map);
});
}
答案 0 :(得分:2)
那是因为你传递的i参数总是最后一个。 试试这个
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Circles</title>
<style>
#map-canvas {
height: 500px;
width: 500px;
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
// Create an object containing LatLng, population.
var cityPoints = {};
cityPoints[0] = {
center: new google.maps.LatLng(41.878113, -87.629798),
id: 0,
addr: 'avenue0',
magnitude: 100000
};
cityPoints[1] = {
center: new google.maps.LatLng(40.714352, -74.005973),
id: 1,
addr: 'avenue1',
magnitude: 100000
};
cityPoints[2] = {
center: new google.maps.LatLng(34.052234, -118.243684),
id: 2,
addr: 'avenue2',
magnitude: 100000
}
var cityCircle;
var infoWindow = new google.maps.InfoWindow();
function initialize() {
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(37.09024, -95.712891),
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
for (i in cityPoints) {
var magnitudeOptions = {
map: map,
center: cityPoints[i].center,
radius: cityPoints[i].magnitude,
id:cityPoints[i].id,
addr:cityPoints[i].addr,
infoWindowIndex: i
};
cityCircle = new google.maps.Circle(magnitudeOptions);
google.maps.event.addListener(cityCircle, 'click', (function(cityCircle, i) {
return function() {
infoWindow.setContent(cityPoints[i].id + " " + cityPoints[i].addr);
infoWindow.setPosition(cityCircle.getCenter());
infoWindow.open(map);
}
})(cityCircle, i));
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
答案 1 :(得分:0)
这称为closure,谷歌有关于如何使用标记进行操作的信息。以下是如何用圆圈做的。 在代码中,&#34;位置&#34;是一个包含我想要绘制为圆圈的所有纬度和经度的数组。
for (i = 0; i < locations.length; i++) {
currentLocation = locations[i].latlng;
var currentLatLng = new google.maps.LatLng(parseFloat(currentLocation[0]),parseFloat(currentLocation[1]) )
var newCircle = new google.maps.Circle({
center: currentLatLng,
radius:10000,
strokeColor:"#0000FF",
strokeOpacity:0.8,
strokeWeight:2,
fillColor:"#0000FF",
fillOpacity:0.4,
map:map,
clickable:true,
});
attachSecretMessage(locations[i].instname+', Tuition: $'+locations[i].tuitionfee02_tf)
}
function attachSecretMessage(tuitionInfo){
var infowindow = new google.maps.InfoWindow({
content: tuitionInfo,
position: currentLatLng
});
myCity.addListener('click', function() {
infowindow.open(map);
});
}