我目前有一个带有位置列表的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>

最佳答案

需要注意的几件事:

您将希望在脚本中包含集群器实用程序库,在这里: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对象。

关于javascript - 使用XML/SQL标记将集群添加到Google Map,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18475223/

10-09 15:48