简体   繁体   English

Google Maps API V3 infoWindow-所有infoWindows显示相同的内容

[英]Google Maps API V3 infoWindow - All infoWindows displaying same content

In my page, latitude, longitude and info window content are present in an array of objects named obj. 在我的页面中,纬度,经度和信息窗口的内容都存在于名为obj的对象数组中。

So if I need the latitude of property of the 1st object, I can call obj[0].latitude. 因此,如果我需要第一个对象的纬度,可以调用obj [0] .latitude。

The infoWindow content for each object is stored in obj[i].text. 每个对象的infoWindow内容存储在obj [i] .text中。

I'm using the below loop to loop through the lat & long values and display the markers and infoWindows. 我正在使用下面的循环遍历纬度和经度值,并显示标记和infoWindows。

  for (x=1; x<=obj.length; x++) { var latlng1 = new google.maps.LatLng(obj[x].latitude, obj[x].longitude);  var marker2 = new google.maps.Marker({ position : latlng1, map : map, icon : prevIcon }); infowindow = new google.maps.InfoWindow(); info = obj[x].text; google.maps.event.addListener(marker2, 'mouseover', function() { infowindow.setContent(info); infowindow.open(map, this); }); } 

for (x=1; x<=obj.length; x++) { var latlng1 = new google.maps.LatLng(obj[x].latitude, obj[x].longitude); var marker2 = new google.maps.Marker({ position : latlng1, map : map, icon : prevIcon }); infowindow = new google.maps.InfoWindow(); info = obj[x].text; google.maps.event.addListener(marker2, 'mouseover', function() { infowindow.setContent(info); infowindow.open(map, this); }); }

for (x=1; x<=obj.length; x++) { var latlng1 = new google.maps.LatLng(obj[x].latitude, obj[x].longitude); var marker2 = new google.maps.Marker({ position : latlng1, map : map, icon : prevIcon }); infowindow = new google.maps.InfoWindow(); info = obj[x].text; google.maps.event.addListener(marker2, 'mouseover', function() { infowindow.setContent(info); infowindow.open(map, this); }); }

The above code works, but all the infoWindows are showing the content of the last obj[i].text. 上面的代码有效,但是所有的infoWindows都显示最后obj [i] .text的内容。

How do I associate the infoWindow content of a particular infoWindow (obj[x].text) to that infoWindow instance only? 如何将特定infoWindow(obj [x] .text)的infoWindow内容仅与该infoWindow实例相关联?

It would be better if someone can point out a solution without using jQuery or any other external libraries. 如果有人可以指出一种解决方案而不使用jQuery或任何其他外部库,那会更好。

Thanks, 谢谢,

Should get you a long way. 应该可以让您走得很远。

function attachMarker( data ) {

    var latLng = new google.maps.LatLng( data.latitude, data.longitude );

    var marker = new google.maps.Marker({
        position : latLng,
        map      : map, // global variable?
        icon     : prevIcon // global variable?
    });

    google.maps.event.addListener(marker, 'mouseover', function() {
       infowindow.setContent( data.text );
       infowindow.open(map, this);
    });

    // add marker here.

}

// afaik, you only need 1 instance of InfoWindow if you only change content.
var infowindow = new google.maps.InfoWindow();

var i = obj.length;
while ( i-- ) {
    attachMarker( obj[ i ] );
}

BGerrissen's comment above helped a lot. BGerrissen在上面的评论很有帮助。

I managed to achieve function closure on the info variable. 我设法在info变量上实现了函数关闭。

The following Google Groups post explains how to. 以下Google网上论坛帖子说明了操作方法。

https://groups.google.com/group/google-maps-api-for-flash/browse_thread/thread/befeb9bf2d1ebcc9 https://groups.google.com/group/google-maps-api-for-flash/browse_thread/thread/befeb9bf2d1ebcc9

Thanks everyone :) 感谢大家 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM