问题
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