简体   繁体   English

使用Google地图标记更改信息窗口中的数据

[英]Changing data in the info window with Google Map markers

I've followed this tutorial to create a custom Google Map. 我已按照本教程创建自定义Google地图。 I've included a few other elements such as linking it up to Wordpress and clustering the markers. 我已经包含了一些其他元素,例如将其链接到Wordpress并聚类标记。

It's all working great apart from the info in the info windows on each marker. 除了每个标记的信息窗口中的信息之外,它都很有效。 I just can't seem to change the info within each one. 我似乎无法改变每个内容的信息。 I thought by changing the following lines it would change it but nothing affects it: 我想通过更改以下行来改变它但没有任何影响它:

var html = "<b>" + name + "</b> <br/>" + address;

This is the working map 这是工作地图

Where can I put in my own custom data into the window? 我在哪里可以将自己的自定义数据放入窗口? Also, if I could style the window on that would be even better. 此外,如果我可以在窗口上设置风格,那就更好了。


It seems the clusterer is the problem, mainly this section, how can I take the html content and place it into the info window? 似乎群集是问题,主要是本节,我如何获取html内容并将其放入信息窗口?

function load() {
  var cluster = [];
  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(52.375599, -3.471680),
    zoom: 8,
    mapTypeId: 'roadmap'
  });
  var infowindow = new google.maps.InfoWindow();
  var min = .999999;
  var max = 1.000002;

  // Change this depending on the name of your PHP file
  downloadUrl("<?php bloginfo('stylesheet_directory'); ?>/phpsqlajax_genxml.php ", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      var name = markers[i].getAttribute("name");
      var address = markers[i].getAttribute("address");
      var type = markers[i].getAttribute("type");

      var offsetLat = markers[i].getAttribute("lat") * (Math.random() * (max - min) + min);
      var offsetLng = markers[i].getAttribute("lng") * (Math.random() * (max - min) + min);

      var point = new google.maps.LatLng(offsetLat, offsetLng);
      var html = "<b>" + name + "</b> <br/>" + address;
      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon,
        shadow: icon.shadow
      });
      google.maps.event.addListener(marker, 'click', (function(marker, i) {
                    return function() {
                        // infowindow.setContent(markers[i].getAttribute("name"));
                        // infowindow.open(map, marker, html);
                        infowindow.setContent(html); infowindow.open(map, marker);
                    }
                })(marker, i));
      cluster.push(marker);
    }
    var mc = new MarkerClusterer(map,cluster);
  });
}

Specifically this, it's not putting the html content through the clusterer... at least this is actually changing the data in the window, just need to output the html content without breaking the clusterer: 具体来说,它不是通过群集器放置html内容...至少这实际上是在更改窗口中的数据,只需输出html内容而不破坏群集器:

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
                    return function() {
                        infowindow.setContent(markers[i].getAttribute("name"));
                        infowindow.open(map, marker, html);
                    }
                })(marker, i));
      cluster.push(marker);

The closest I have it so far is this but it shows the same info for every marker. 到目前为止我最接近的是它,但它显示了每个标记的相同信息。 It's showing the html content: 它显示了html内容:

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
                    return function() {
                        // infowindow.setContent(markers[i].getAttribute("name"));
                        // infowindow.open(map, marker, html);
                        infowindow.setContent(html); infowindow.open(map, marker);
                    }
                })(marker, i));
      cluster.push(marker);

Can't believe I didn't think of this sooner!! 简直不敢相信我没想到这个!!

It just a case of building the string in the listener. 它只是在侦听器中构建字符串的情况。

  // Change this depending on the name of your PHP file
  downloadUrl("<?php bloginfo('stylesheet_directory'); ?>/phpsqlajax_genxml.php ", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      var type = markers[i].getAttribute("type");

      var offsetLat = markers[i].getAttribute("lat") * (Math.random() * (max - min) + min);
      var offsetLng = markers[i].getAttribute("lng") * (Math.random() * (max - min) + min);

      var point = new google.maps.LatLng(offsetLat, offsetLng);
      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon,
        shadow: icon.shadow
      });
      google.maps.event.addListener(marker, 'click', (function(marker, i) {
                    return function() {
                        var name = markers[i].getAttribute("name");
                        var address = markers[i].getAttribute("address");
                        var html = "<b>" + name + "</b> <br/>" + address;
                        infowindow.setContent(html);
                        infowindow.open(map, marker, html);
                        // infowindow.setContent(html);
                        // infowindow.open(map, marker);
                    }
                })(marker, i));
      cluster.push(marker);
    }

If I'm reading it correctly. 如果我正确阅读它。 You are trying to set content 'after' setting the marker. 您正尝试在设置标记后设置内容。

This should be the other way around. 这应该是另一种方式。 Move the piece where you set the html to before you push it to the cluster. 将设置html的部分移动到将其推送到集群之前。


edit: 编辑:

for (var i = 0; i < markers.length; i++) {
  var name = markers[i].getAttribute("name");
  var address = markers[i].getAttribute("address");
  var type = markers[i].getAttribute("type");

  var offsetLat = markers[i].getAttribute("lat") * (Math.random() * (max - min) + min);
  var offsetLng = markers[i].getAttribute("lng") * (Math.random() * (max - min) + min);

  var point = new google.maps.LatLng(offsetLat, offsetLng);
  //var html = "<b>" + name + "</b> <br/>" + address;
  var infowindow = new google.maps.InfoWindow({content: "<b>" + name + "</b> <br/>" + address});

  var icon = customIcons[type] || {};
  var marker = new google.maps.Marker({
    map: map,
    position: point,
    icon: icon.icon,
    shadow: icon.shadow
  });
  //google.maps.event.addListener(marker, 'click', (function(marker, i) {
  //              return function() {
  //                  infowindow.setContent(markers[i].getAttribute("name"));
  //                  infowindow.open(map, marker, html);
  //              }
  //          })(marker, i));
  google.maps.event.addListener(marker, 'click',  function(marker, i){infowindow.open(map,marker);})(marker, i);

  cluster.push(marker);
}

Not sure about the (marker, i) pieces. 不确定(标记,i)件。 I assume they are used by the marker manager to keep trakc of what's what. 我假设标记管理器使用它们来保持什么是trakc。 Those two changes (I commented out your lines and added one below) seem to be the next logical step. 这两个变化(我注释掉了你的线并在下面添加了一个)似乎是下一个合乎逻辑的步骤。

i had a similar problem, i figure this out, to change the content in marker infowindow 我有一个类似的问题,我想出来,改变标记infowindow的内容

    var marker = new MarkerWithLabel({
        id: "costume_ID",/*<---this is the start of the trick*/
        position: new google.maps.LatLng(lat,lon),
        icon: "../images/icon-bla.png",
        map: map,
        title: 'bla',
        labelContent: 'bla,bla,
        labelClass: "labels", 
        labelStyle: {opacity: 1.0}
    })
    google.maps.event.addListener(marker, 'click', (function() {
        return function(){
        infowindow.setContent(infor(this.id));/*<--- here is the trick*/
        infowindow.open(map, this);
        map.setCenter(this.getPosition());
    }});

and than set the function that will output whatever you whant, if you have the info in variables. 如果你在变量中有信息,那么设置输出你想要的任何东西的函数。

    function infor(im){
        return "<div><b>INFOWINDOW</b><br>Date: "+var[im].date+"<br>Sync:  "+var[im].sync+"...bla,bla,bla</div>";
    }/*THIS FUNCTION RETURN THE STING TO SHOW ION THE INFOWINDOW*/

The issue is with your closure. 问题出在你的关闭上。 The for loop isn't handling your scope correctly. for循环未正确处理您的范围。 Extract all the code from inside the for loop into a separate function and the closure should work. for循环内部的所有代码解压缩到一个单独的函数中,闭包应该可以工作。

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

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