0

我在为多个圆形叠加层中的每一个添加一个信息窗口时遇到问题。当我单击一个圆圈时,会出现一个信息窗口,但不是在我单击的圆圈上,而是在最后一个添加的圆圈上。似乎每次执行循环时,都会将事件侦听器添加到每个圆圈中,但信息窗口的内容是从添加的最后一个圆圈派生的。

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);
     });

}
4

2 回答 2

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>
于 2013-05-14T08:43:09.780 回答
0

这称为闭包,谷歌有关于如何使用标记的信息。以下是如何使用圆圈进行操作。在代码中,“位置”是一个数组,其中包含我想要绘制为圆圈的所有纬度和经度。

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);
  });
}
于 2016-07-29T18:44:58.523 回答