1

我敢肯定这是非常基本的东西,但我已经看了一个小时,但我看不到它。我很欣赏一双新鲜的眼睛。这是一个非常基本的 Google Maps API v3 项目,几乎是从说明书中逐字复制的,它将 MySQL 数据库中的数据加载到 XML 文件中,然后将 XML 文件中的数据加载到地图中。

以下是 XML 文件中的 3 个示例标记:

<markers>
<marker name="Total Beverage" type="2" address="9359 Sheridan Blvd Wesminster, CO 80031" lat="-105.053293" lon="39.8658159"/>
<marker name="Union Jack Liquor" type="2" address="1160 S Boulder Road Louisville, CO 80027" lat="-105.1301647" lon="39.9868051"/>
<marker name="Argonaut Wine and Liquor" type="2" address="760 E Colfax  Denver, CO 80203" lat="-104.9785574" lon="39.7398985"/>
</markers>

这是生成地图的代码。

<!DOCTYPE html >
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Big Choice Sample Map</title>
    <style type="text/css">
        html { height: 100% }
        body { height: 100%; margin: 0; padding: 0 }
        #map { height: 100% }
    </style>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">

    var customIcons = {
      0: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_yellow.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      },
      1: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      },
      2: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      }
    };

    function load() {
      var latlng = new google.maps.LatLng(39.909939, -105.078369);
      var options = {
        zoom: 12,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById("map"), options);
      var infoWindow = new google.maps.InfoWindow;

      downloadUrl("phpsql_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 type = parseInt(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>";
          var icon = customIcons[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            title: name,
            icon: icon.icon,
            shadow: icon.shadow
          });
          bindInfoWindow(marker, map, infoWindow, html);
        }
      });
    }

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

    function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null);
    }

    function doNothing() {}

    //]]>

  </script>

  </head>

  <body style="margin:0px; padding:0px;" onload="load()">
    <div id="map" style="width:100%; height:100%"></div>
  </body>

</html>

地图在正确的位置居中,并缩放到正确的级别,但标记不显示。感谢您的任何指示。

4

1 回答 1

4

您的 xml 中有向后的纬度和经度:

lat="-105.053293" lon="39.8658159"

您的地图集中在:

var latlng = new google.maps.LatLng(39.909939, -105.078369);
于 2013-06-10T05:46:41.653 回答