2

我知道这是一个重复的问题,

我在基于 Django 网络的应用程序上使用谷歌地图 v3。我在哪里使用Markers, Infowindow and polyline。一切正常,除了当我单击标记以通过信息窗口显示内容时,之前打开的信息窗口没有关闭。

我正在发布我的地图代码(仅脚本部分或有用的部分):

var marker = add_marker(flightPlanCoordinates[i][0], flightPlanCoordinates[i][1],"Event Detail",myHtml);

myHtml是一个包含信息窗口内容的变量。我没有在这里定义变量。所以忽略它。

    marker.setMap(map);
    }

    var flightPath = new google.maps.Polyline({
                 path: flightPlanCoordinatesSet,
                 strokeColor: "#FF0000",
                 strokeOpacity: 1.0,
                 strokeWeight: 2
                  });
    flightPath.setMap(map);
}

function add_marker(lat,lng,title,box_html) {
var infowindow = new google.maps.InfoWindow({
    content: box_html
});

var marker = new google.maps.Marker({
      position: new google.maps.LatLng(lat,lng),
      map: map,
      title: title
});

google.maps.event.addListener(marker, 'click', function() {
  infowindow.open(map,this);
});   

return marker;
}
4

1 回答 1

3

而不是多个 infoWindows 只使用一个实例。

单击标记时,首先关闭 infoWindow,然后设置新内容并打开 infoWindow。

function add_marker(lat,lng,title,box_html) 
{
  //create the global instance of infoWindow 
  if(!window.infowindow)
  {
    window.infowindow=new google.maps.InfoWindow();
  } 

  var marker = new google.maps.Marker({
      position: new google.maps.LatLng(lat,lng),
      map: map,
      title: title
  });

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.close();
    infowindow.setContent(box_html);
    infowindow.open(map,this)
  });   

  return marker;
}
于 2012-07-11T08:17:11.183 回答