问题描述
仍然徘徊在我不明白的问题上。我不明白为什么这不起作用,我相信这是非常简单的事情。我已经改变了一些东西,仍然没有。我试图让用户点击地图上的任何地方,并且一个infoWindow会告诉他们该地点的纬度/经度。谢谢
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);
});
推荐答案
这里有很多问题。首先,我不会使用 window
作为变量名称。其次,当你创建InfoWindow时,它需要被大写
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()
第三,您将事件对象传递给InfoWindow创建函数。传递事件对象很糟糕。所以现在,在InfoWindow函数中,location指向事件对象,而不是位置。您应该传递event.latLng。您还需要调用InfoWindow上的open方法。
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);
});
这篇关于Google Maps API v3信息泡泡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!