2

我在 php mysql 方面很有经验,但 javascript 让我摸不着头脑。我正在尝试将谷歌地图添加到我的网站,以显示我的数据库中的照片是在哪里拍摄的。我让静态谷歌地图工作,但由于 url 字符限制,它只会显示 37 个标记。我按照 google maps api docs 上的教程进行操作,我有一张地图,可以显示我数据库中图像的所有坐标。我的问题是,我一生都无法弄清楚如何使地图自动居中并自动缩放以适合我的所有标记。我的地图测试网站位于maptest。我找到了这篇关于如何自动居中/缩放我的地图的教程,无论我把他的代码放在哪里都会出错。这是我的地图没有自动缩放/居中的代码:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Maps AJAX + mySQL/PHP Example</title>
    <script src="http://maps.google.com/maps/api/js?             key=AIzaSyDnMkDkoCHNE7BG4eobjeMJdWWZtdZvzeg&sensor=false"
        type="text/javascript"></script>
    <script type="text/javascript">
    //<![CDATA[

  var customIcons = {
  restaurant: {
    icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
    shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
  },
  bar: {
    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 map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(42.293564,-39.07617),
    zoom: 2,
    mapTypeId: 'roadmap'
  });
  var infoWindow = new google.maps.InfoWindow;

  // Change this depending on the name of your PHP file
  downloadUrl("phpsqlajax_genxml3.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 point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("lat")),
          parseFloat(markers[i].getAttribute("lng")));
      var html = "<b>" + name + "</b> <br/>";
      //var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        //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 onload="load()">
   <div id="map" style="width: 600px; height: 400px"></div>
    </body>
   </html>
4

1 回答 1

3

试过这个?

var fitToMarkers = function(markers) {
    var bounds = new google.maps.LatLngBounds();
    var length = markers.length;
    for (var i = 0; i < length; i++) {
        bounds.extend(new google.maps.LatLng(markers[i].lat, markers[i].lng));
        map.fitBounds(bounds);
    }
};
于 2012-10-19T14:42:14.807 回答