infoWindow没有显示数据库导入

时间:2014-04-30 04:11:29

标签: google-maps google-maps-api-3

我从数据库导入我的标记的Lat和Lon。我试图只显示垃圾内容的infoWindow只是为了看它是否有效,我已经尝试了一切,但似乎无法使它工作。有什么帮助吗?

var contentString;

contentString = '<div id="content">'+
  '<div id="siteNotice">'+
  '</div>'+
  '<h1 id="firstHeading" class="firstHeading">Circle K</h1>'+
  '<div id="bodyContent">'+
  '<p><b>Gas Station Type: Shell</b>'+'<br><b>Gas Prices</b>: Regular 3.08 Midgrade 3.39'+
  ' Premium '+price+' Diesel 3.35'+'<br>3289 Highland Road, Baton Rouge, LA, 70802'+
  '</p>'+
  '</div>'+
  '</div>';
  //contentString = <p><b> Gas </b></p>

  var infoWindow = new google.maps.InfoWindow(
    {
      content: 'Hello'
      //content: contentString
    });

function load() {
  var map = new google.maps.Map(document.getElementById("map-canvas"), {
    center: new google.maps.LatLng(30.418313, -91.176557),
    zoom: 13,
    mapTypeId: 'roadmap'
  });


  downloadUrl("data.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("GID");
      var address = markers[i].getAttribute("GasStationType");
      //var type = markers[i].getAttribute("type");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("Lat")),
          parseFloat(markers[i].getAttribute("Lon")));
      var html = "<b>" + name + "</b> <br/>" + address;
      //var icon = customIcons[address] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        //content: 'Hello'
        //icon: icon.icon,
        //shadow: icon.shadow
      });
      bindInfoWindow(markers, map, infoWindow, html);
    }
  });
}

function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(marker);
    infoWindow.open(map, marker);
  });
}

1 个答案:

答案 0 :(得分:2)

bindInfoWindow(markers, map, infoWindow, html);标记是一个数组。 bindInfowindow需要一个标记。并且infoWindow.setContent需要一个字符串,而不是google.maps.Marker对象。固定功能:

function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
}

改变这个:

bindInfoWindow(markers, map, infoWindow, html);

为:

bindInfoWindow(marker, map, infoWindow, html);

working fiddle