谷歌地图js api版本3多边形infowindow在错误的位置

时间:2012-06-20 10:10:38

标签: javascript google-maps google-maps-api-3 polygon

我在循环中有这段代码:

      var points = [new google.maps.LatLng(lat1, lon1),
          new google.maps.LatLng(lat2, lon1),
          new google.maps.LatLng(lat2, lon2),
          new google.maps.LatLng(lat1, lon2),
          new google.maps.LatLng(lat1, lon1)
          ];
      bounds.extend(new google.maps.LatLng(lat1, lon1));
      bounds.extend(new google.maps.LatLng(lat2, lon1));
      bounds.extend(new google.maps.LatLng(lat2, lon2));
      bounds.extend(new google.maps.LatLng(lat1, lon2));
      var polygon = new google.maps.Polygon({
                        paths: points,
                        strokeColor: "#f33f00",
                        strokeOpacity: 1,
                        strokeWeight: 1,
                        fillColor: "#ff0000",
                        fillOpacity: 0.2
                    });

    var x1 = Math.min(lon1,lon2);
    var y1 = Math.min(lat1,lat2);
    var x2 = Math.max(lon1,lon2);
    var y2 = Math.max(lat1,lat2);
    var x = x1 + ((x2 - x1) / 2);
    var y = y1 + ((y2 - y1) / 2);
    var cp = new google.maps.LatLng(y,x);
    var infoWindow = new google.maps.InfoWindow();
                    infoWindow.setPosition(cp);
                    infoWindow.setContent(html);

   google.maps.event.addListener(polygon, "click", function() {
                        infoWindow.open(map);
                    });

                    polygon.setMap(map);

我的问题是当我点击每个多边形时,infowindow会在所有多边形的相同位置打开。

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

您正在覆盖每个循环上的infoWindow对象。

将与infoWindow(cp,html)相关的属性存储在polygon-objects中,并使用存储的属性在click-function中调用setPosition()setContent()

答案 1 :(得分:0)

我所做的是,全局设置地图,通过调用执行相同工作的循环外的函数来替换代码块,如下所示:

function createPolygon(lat1, lon1, lat2, lon2, html)
{
     var points = [new google.maps.LatLng(lat1, lon1),
      new google.maps.LatLng(lat2, lon1),
      new google.maps.LatLng(lat2, lon2),
      new google.maps.LatLng(lat1, lon2),
      new google.maps.LatLng(lat1, lon1)
      ];
  bounds.extend(new google.maps.LatLng(lat1, lon1));
  bounds.extend(new google.maps.LatLng(lat2, lon1));
  bounds.extend(new google.maps.LatLng(lat2, lon2));
  bounds.extend(new google.maps.LatLng(lat1, lon2));
  var polygon = new google.maps.Polygon({
                    paths: points,
                    strokeColor: "#f33f00",
                    strokeOpacity: 1,
                    strokeWeight: 1,
                    fillColor: "#ff0000",
                    fillOpacity: 0.2
                });

var x1 = Math.min(lon1,lon2);
var y1 = Math.min(lat1,lat2);
var x2 = Math.max(lon1,lon2);
var y2 = Math.max(lat1,lat2);
var x = x1 + ((x2 - x1) / 2);
var y = y1 + ((y2 - y1) / 2);
var cp = new google.maps.LatLng(y,x);
var infoWindow = new google.maps.InfoWindow();
                infoWindow.setPosition(cp);
                infoWindow.setContent(html);

 google.maps.event.addListener(polygon, "click", function() {
                    infoWindow.open(map);
                });

                polygon.setMap(map);
}