0

我在这里遇到了一个混乱的情况,希望有人可以帮助我解决这个话题已经在 Stackoverflow 中讨论过的事实。

我正在使用循环来读取 XML 并基于该 XML 的节点创建标记,这是可行的。此外,在该循环内,我调用位于该循环外的一个函数,为每个标记创建一个 InfoWindow,其中包含从 XML 中提取的内容。

  • 我在循环外部和函数内部创建 InfoWindows,因为如果我不这样做,无论在哪个标记中单击,它总是在地图的最后添加标记上方显示相同的 InfoWindows。
  • 如果我定义 InfoWindows 全局所有 InfoWindows 显示最后创建的标记的相同内容。

这是代码:

function xml_Leer() {       

    function agregar_datos(marcador, map, direccion, telefono, dias_habiles, sabado, domingo) {
        var datos = new google.maps.InfoWindow({content: "<h2>"+nombre+"</h2><p>"+direccion+"</p><p>"+telefono+"</p><ul><li>Lunes a Viernes: "+dias_habiles+"</li><li>S&aacute;bados: "+sabado+"</li><li>Domingos: "+domingo+"</li></ul>"});
        google.maps.event.addListener(marcador, 'click', function() {datos.open(map,marcador);});
    } 

    var iglesias = xml.getElementsByTagName("iglesia");

    for(var i=0; i<iglesias.length; i++) {
        var nombre = iglesias[i].getElementsByTagName("nombre")[0].childNodes[0].nodeValue;

        var direccion = iglesias[i].getElementsByTagName("direccion")[0].childNodes[0].nodeValue;
        var telefono = iglesias[i].getElementsByTagName("telefono")[0].childNodes[0].nodeValue;

        var dias_habiles = iglesias[i].getElementsByTagName("horarios")[0].getElementsByTagName("dias_habiles")[0].childNodes[0].nodeValue;
        var sabado = iglesias[i].getElementsByTagName("horarios")[0].getElementsByTagName("sabado")[0].childNodes[0].nodeValue;
        var domingo = iglesias[i].getElementsByTagName("horarios")[0].getElementsByTagName("domingo")[0].childNodes[0].nodeValue;

        var tipo = iglesias[i].getAttribute("tipo");


        var ubicacion = new google.maps.LatLng(iglesias[i].getAttribute("lat"), iglesias[i].getAttribute("lng"));

        var marcador = new google.maps.Marker({position: ubicacion, animation: google.maps.Animation.DROP, map: map, title:nombre });

        agregar_datos(marcador, map, direccion, telefono, dias_habiles, sabado, domingo); 
    }

}

如果这没有很好的编码,我应该如何创建 InfoWindow 考虑到我需要从 XML 中获取的信息显示在 InfoWindow 的内容中?

提前致谢。


此代码有效,但我不知道如何,只是...有效。

var datos = new google.maps.InfoWindow();
function agregar_datos(marcador, map, direccion, telefono, dias_habiles, sabado, domingo) {
    var datos = new google.maps.InfoWindow({content: "<h2>"+nombre+"</h2><p>"+direccion+"</p><p>"+telefono+"</p><ul><li>Lunes a Viernes: "+dias_habiles+"</li><li>S&aacute;bados: "+sabado+"</li><li>Domingos: "+domingo+"</li></ul>"});
    google.maps.event.addListener(marcador, 'click', function() { if(window.datos){window.datos.close();} window.datos=datos; datos.open(map,marcador);});
} 
4

2 回答 2

0

我怀疑这是因为 javascript 关闭问题,看这里,这是完全相同的问题! https://stackoverflow.com/a/8326203/684229

您有问题,因为当 marcador click 事件处理程序启动时,它使用marcador函数调用中的变量,但这已经更改为 for 循环中的最后一个标记。

只需尝试更改函数中的代码agregar_datos()以使用this而不是marcador

google.maps.event.addListener(marcador, 'click', function() {datos.open(map, this);});
于 2013-01-22T22:57:59.393 回答
0

创建 1 个全局变量,用于存储最后打开的信息窗口。当你打开一个新的 infoWindow 时,你可以访问这个全局变量并关闭窗口:

在全球范围内的某处:

var datos;

在 agregar_datos() 中:

删除第二行并添加:

google.maps.event.addListener(marcador, 'click', 
   function() {
     if(window.datos){window.datos.close();}
     window.datos=datos;
     datos.open(map,marcador);
});

更好的解决方案:仅使用 1 个 infoWindow:

在全球范围内的某处:

var datos=new google.maps.InfoWindow();

在 agregar_datos() 中:

删除行:

var datos = new google.maps.InfoWindow(/*....*/);

并添加:

google.maps.event.addListener(marcador, 'click', 
       function() {
         window.datos.close();
         window.datos.setContent("put your content here");
         datos.open(map,marcador);
    });
于 2013-01-22T23:39:29.580 回答