【问题标题】:Google Maps API v3 Info Bubble [duplicate]Google Maps API v3 信息气泡 [重复]
【发布时间】:2026-01-27 09:05:02
【问题描述】:

可能重复:
InfoWindow not Displaying

还在纠结一个我不明白的问题。我不明白为什么这不起作用,我相信这是非常简单的事情。我改变了一些东西,但仍然没有。我试图让用户点击地图上的任何地方,infoWindow 会告诉他们该位置的纬度/经度。谢谢

var window;
function InfoWindow(location) {
  if ( window ) {
    window.setPosition(location);
  } else {
    window = new google.maps.infoWindow({
      position: location,
      content: "sam"
    });
  }
}

google.maps.event.addListener(map, 'click', function(event) {
  InfoWindow(event);
});

【问题讨论】:

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


    【解决方案1】:

    这里有多个问题。首先,我不会使用window 作为变量名。其次,在创建 InfoWindow 时,它需要大写

    new google.maps.InfoWindow()
    

    第三,您将事件对象传递给您的 InfoWindow 创建函数。传递事件对象是不好的。所以现在,在 InfoWindow 函数中,位置是指事件对象,而不是位置。您应该改为传递 event.latLng。您还需要在 InfoWindow 上调用 open 方法。

    var infoWindow;
    function InfoWindow(location) {
      if ( infoWindow ) {
        infoWindow.setPosition(location);
      } else {
        infoWindow = new google.maps.InfoWindow({
          position: location,
          content: "sam"
        });
      }
      infoWindow.open();
    }
    google.maps.event.addListener(map, 'click', function(event) {
      InfoWindow(event.latLng);
    });
    

    【讨论】: