简体   繁体   English

Google Maps API,所有标记均打开相同的信息窗口

[英]Google Maps API, all markers opening the same infowindow

I've got a page that retrieves a bunch of locations and some data about their associated markers and puts them on a Google Maps map. 我有一个页面,该页面可检索一堆位置以及有关其关联标记的一些数据,并将其放置在Google Maps地图上。 Each one is supposed to pop up its own little message when clicked on. 每个人都应该在单击时弹出自己的小消息。 However, clicking on ANY of them makes the most recently added message pop up at the most recently added marker. 但是,单击其中的任何一个都会使最新添加的消息在最新添加的标记处弹出。 What gives? 是什么赋予了? Am I not scripting the click event properly? 我是否无法正确编写Click事件的脚本? Here's the relevant code: 以下是相关代码:

var xmlDoc;
    if (window.XMLHttpRequest)
    {
    xmlDoc=new window.XMLHttpRequest();
    xmlDoc.open("GET","locs.php",false);
    xmlDoc.send("");
    xmlDoc=xmlDoc.responseXML;
    }
    // IE 5 and IE 6
    else if (ActiveXObject("Microsoft.XMLDOM"))
    {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.load("locs.php");
    }

    var pins = xmlDoc.getElementsByTagName("pin");
    //alert(pins);

    for (i=0;i<pins.length;i++)
    {
       //alert(pins[i].getElementsByTagName("lat")[0].childNodes[0].nodeValue);
       var point = new GLatLng( pins[i].getElementsByTagName("lat")[0].childNodes[0].nodeValue, 
                                pins[i].getElementsByTagName("lon")[0].childNodes[0].nodeValue);
       var colord;
       var curgender = pins[i].getElementsByTagName("gender")[0].childNodes[0].nodeValue;
       if(curgender == "Male")
       {colord = blueOpt;}else if(curgender=="Female"){colord = pinkOpt;}else{colord = purpleOpt;}

       var marker = new GMarker(point, colord);
       var mess =  pins[i].getElementsByTagName("message")[0].childNodes[0].nodeValue;

       GEvent.addListener(marker, "click", function() {
         marker.openInfoWindowHtml(mess);
       });

       map.addOverlay(marker);

    }
  }

Daff is right about the scope. Daff对范围是正确的。 One alternative, however, is to use bindInfoWindowHtml() instead of a listener with openInfoWindowHtml(). 但是,一种替代方法是使用bindInfoWindowHtml()代替带有openInfoWindowHtml()的侦听器。 Just replace the listener with this code: 只需用以下代码替换侦听器:

marker.bindInfoWindowHtml(mess);


UPDATE: As a sidenote - because of closure , any variable created in a for loop will be remembered. 更新:作为旁注-由于闭包 ,将记住在for循环中创建的任何变量。 A direct solution to the problem is to make a separate function to create the Listener. 解决该问题的直接方法是创建一个单独的函数来创建侦听器。

Replace the listener with 将监听器替换为

function createMarker(marker, mess) {
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(mess);
    });
}

and add the function: 并添加功能:

 function createMarker(marker, mess) { GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(mess); }); } 

I had this once, too. 我也有一次。 It is a scoping issue. 这是一个范围界定的问题。 I had to change my structure a little bit but maybe in your case changing the callback could already help: 我不得不稍微改变一下结构,但也许在您的情况下,更改回调可能已经有所帮助:

GEvent.addListener(marker, "click", function() {
 marker.openInfoWindowHtml(pins[i].getElementsByTagName("message")[0].childNodes[0].nodeValue;);
});

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

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