Google地图InfoWindow无法在点击事件中打开 [英] Google maps InfoWindow not opening on click event
问题描述
我在Google地图api(v3)和InfoWindows的某些问题上没有打开它们附加的标记点击事件。当我调试JavaScript时,Markers和InfoWindows看起来已经正确创建,所以我假设我在添加click事件监听器时做了一些错误。
以下是标记等的添加发生。任何人都可以看到问题所在?
$。post(/ Club / SearchClubsByLocation,{latitude:searchLat,longitude :searchLng},
函数(俱乐部){
$ .each(俱乐部,功能(我,俱乐部){
var LL =新google.maps.LatLng(club.Latitude,club。经度);
pointArray.push(LL);
var infoWindow = new google.maps.InfoWindow({
content:club.ClubName +HELLO !!!
});
var marker = new google.maps.Marker({
map:map,
position:LL
});
google.maps.event.addListener(marker,'click',function(){
infowindow.open(map,marker);
});
markerArray.push(marker);
});
for(i in pointArray){
bounds.extend(pointArray [i]);
}
map.fitBounds(bounds);
pointArray = [];
},
json
);
感谢您的任何建议,
Rich
我终于通过在post函数之外创建一个InfoWindow并将一个点击侦听器添加到标记打开InfoWindow
$。post(/ Club / SearchClubsByLocation,{latitude:searchLat,longitude:searchLng},
功能(俱乐部){
$ .each(俱乐部,功能(我,俱乐部){
var LL =新google.maps.LatLng(club.Latitude,club.Longitude);
pointArray.push(LL);
var marker = new google.maps.Marker({
map:map,
position:LL
}) ;
addInfoWindow(marker,club.ClubName);
markerArray.push(marker);
});
for(我在pointArray){
bounds.extend(pointArray [i]);
}
map.fitBounds(bounds);
pointArray = [];
},
json
);
$ b $ function addInfoWindow(marker,content){
var infoWindow = new google.maps.InfoWindow({$ b $ content:content
});
google.maps.event.addListener(marker,'click',function(){
infoWindow.open(map,marker);
});
$ b 看来你需要一个单独的函数来添加多个InfoWindows, / p>
干杯
丰富
I am having some problems with google maps api (v3) and InfoWindows no opening on the marker's click event that they are attached to. When I debug the javascript the Markers and InfoWindows look to have been created correctly, so I am assuming that I am doing something wrong when adding the click event listener.
Below is where the adding of Markers etc happens. Can anyone see what the problem may be?
$.post("/Club/SearchClubsByLocation", { latitude: searchLat, longitude: searchLng },
function (clubs) {
$.each(clubs, function (i, club) {
var LL = new google.maps.LatLng(club.Latitude, club.Longitude);
pointArray.push(LL);
var infoWindow = new google.maps.InfoWindow({
content: club.ClubName + " HELLO!!!"
});
var marker = new google.maps.Marker({
map: map,
position: LL
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});
markerArray.push(marker);
});
for (i in pointArray) {
bounds.extend(pointArray[i]);
}
map.fitBounds(bounds);
pointArray = [];
},
"json"
);
Thanks for any suggestions,
Rich
解决方案 I finally solved it in the by having a function outside the post function that creates an InfoWindow and adds a click listener to a marker to open the InfoWindow
$.post("/Club/SearchClubsByLocation", { latitude: searchLat, longitude: searchLng },
function (clubs) {
$.each(clubs, function (i, club) {
var LL = new google.maps.LatLng(club.Latitude, club.Longitude);
pointArray.push(LL);
var marker = new google.maps.Marker({
map: map,
position: LL
});
addInfoWindow(marker, club.ClubName);
markerArray.push(marker);
});
for (i in pointArray) {
bounds.extend(pointArray[i]);
}
map.fitBounds(bounds);
pointArray = [];
},
"json"
);
function addInfoWindow(marker, content) {
var infoWindow = new google.maps.InfoWindow({
content: content
});
google.maps.event.addListener(marker, 'click', function () {
infoWindow.open(map, marker);
});
}
It seems that you need a separate function to add multiple InfoWindows as seen here
Cheers
Rich
这篇关于Google地图InfoWindow无法在点击事件中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!