使用XML / SQL标记将群集添加到Google Map

时间:2013-08-27 20:49:56

标签: javascript sql google-maps google-maps-api-3 map

我目前有一个带有位置列表的SQL表,它们被填充到Google Map上。

我想按照此演示中所示对标记进行聚类:http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/examples/advanced_example.html

我的地图脚本如下:

 <script src="http://maps.google.com/maps/api/js?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(51.854018, 0.974768),
        zoom: 11,
        mapTypeId: 'hybrid'
      });     

      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 dayticket = markers[i].getAttribute("dayticket");
          var clubmembership = markers[i].getAttribute("clubmembership");
          var address = markers[i].getAttribute("address");
          var contactdetails = markers[i].getAttribute("contactdetails");
          var watertype = markers[i].getAttribute("watertype");
          var disabledaccess = markers[i].getAttribute("disabledaccess");
          var lastupdated = markers[i].getAttribute("lastupdated");
          var type = markers[i].getAttribute("type");
          var point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lng")));
          var html = "<h3><center>" + name + "</center></h3>" + "<b>Day Ticket: </b>" + dayticket + "</br>" + "<b>Club Membership: </b>" + clubmembership + "</br></br>" + "<b>Water Type: </b>" + watertype + "</br>" + "<b>Disabled Access: </b>" + disabledaccess + "</br>" + "<b>Contact Details: </b>" + contactdetails + "</br>" + "<b>Address: </b>" + address + "</br>" + "<b>Last Updated: </b>" + lastupdated;
          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>

1 个答案:

答案 0 :(得分:1)

有几点需要注意:

您需要在脚本中包含clusterer实用程序库,此处为:http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/src/markerclusterer.js

这里的文档(http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/docs/examples.html)有一些很好的例子来说明如何实现群集器。您的标记数据是来自SQL,XML还是其他任何地方都无关紧要。在创建新标记对象时,您需要能够将它们传递给MarkerClusterer对象。