JavaScript google maps API - 一次打开一个infowindow ..

时间:2010-08-18 13:30:10

标签: javascript events google-maps-api-3 infowindow

这个与Google地图API v3中的infowindow有关的问题。

目前我循环此功能并放置标记..

function addPostCode(zip, html) 
{
   geocoder.geocode( { 'address': zip}, function(results, status) 
   {
      if (status == google.maps.GeocoderStatus.OK)
      {
         map.setCenter(results[0].geometry.location);
         var marker = new google.maps.Marker({
         map: map,
         position: results[0].geometry.location,
         name: zip
      });
});

现在我想添加一个带有唯一HTML的信息窗口,但是我想要以下行为..

当一个事件打开一个信息窗口时,任何当前的信息窗口都将关闭,只留下新信息。

这可能吗?我该怎么办呢?找到关于这个问题的文件证明是困难的。

4 个答案:

答案 0 :(得分:3)

在初始化中创建一个infowindow。在您想要打开信息窗的事件/监听器中,您需要设置内容并在地图上的标记/位置打开信息窗。

// Initialize infowindow
var infowindow = new google.maps.InfoWindow({content: ''});

function add_marker(point, name, content)
{
   var marker = new google.maps.Marker({
      map: map,
      position: point,
      dragable: false,
      clickable: true,
      name: name
   });
   marker.content = content;
   google.maps.event.addListener(marker, 'click', function()
   {
      infowindow.content = marker.content;
      infowindow.open(map, marker);
   });
   return marker;
};

function addPostCode(zip, html) 
{
   geocoder.geocode( { 'address': zip}, function(results, status) 
   {
      if (status == google.maps.GeocoderStatus.OK)
      {
         map.setCenter(results[0].geometry.location);
         var marker = add_marker(results[0].geometry.location, zip, html)
      });
});

这个问题和答案帮助我解决了一个或多个信息窗口问题:

Google Maps API v3 adding an InfoWindow to each marker

答案 1 :(得分:1)

var currentInfoWindow = ''; //Global variable

google.maps.event.addListener(marker, 'click', function()
   {
      if(currentInfoWindow != '')
      { 
        currentInfoWindow.close();
        currentInfoWindow = '';
      }
      var infoWindow = new google.maps.InfoWindow({content: 'COntent'});
      infowindow.open(map, marker);
      currentInfoWindow = infowindow;
   });

答案 2 :(得分:0)

最好不要向Google Groups for the Google Maps API v3询问此问题以及其他相关问题。

我没有使用过API v3,但据我所知,你一次只能打开一个信息窗口,所以这会自动发生。

答案 3 :(得分:0)

有两种方法可以做到这一点......第一种方法是创建一个信息窗口,只需使用信息窗口的.setOptions方法来更新content

第二种方法是在代码中设置模块级变量以包含“活动”窗口...然后如果打开任何infoWindow,在打开之前调用它的.close方法新的。