Google Maps API v3 Info Bubble [duplicate]

风格不统一 提交于 2020-01-05 07:55:13

问题


Possible Duplicate:
InfoWindow not Displaying

Still lingering on a problem that I do not understand. I do not see why this would not work and I'm sure it's something extremely simple. I've changed a few things and still nothing. I am trying to have a user click anywhere on the map and an infoWindow will tell them the lat/lng of that location. Thanks

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

回答1:


There are multiple issues here. First, I would not use window as a variable name. Second, when you are creating the InfoWindow it needs to be capitalized

new google.maps.InfoWindow()

Third, you are passing the event object to your InfoWindow creation function. Passing the event object around is bad. So now, in the InfoWindow function, location refers to the event object, not the location. You should pass event.latLng instead.You also need to call the open method on the InfoWindow.

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


来源:https://stackoverflow.com/questions/11671571/google-maps-api-v3-info-bubble

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!