Google Maps Marker Clusterer无法正常运行

cli*_*ray 5 javascript google-maps google-maps-api-3 markerclusterer

我有以下代码尝试让MarkerClusterer库适用于我的Google地图但由于某种原因它不会改变任何东西.我在那里有一些jinja2用于循环,但这一切都正常.你能看到任何错误吗?

<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <style type="text/css">
          html { height: 100% }
          body { height: 100%; margin: 0; padding: 0 }
          #map_canvas { height: 100% }
        </style>
        <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyD-pLsocZXv5mYwJsSxMghJncxa6iklFUU&sensor=false"></script>
        <script type="text/javascript" src="/static/js/markerclusterer.js"></script>
        <script type="text/javascript">

    var map;    

    function initialize() {

        var centerlocation = {{centerlocation|json_encode|safe}};   
        var LatLng = centerlocation.replace("(", "").replace(")", "").split(", ")
        var Lat = parseFloat(LatLng[0]);
        var Lng = parseFloat(LatLng[1]);

        var zoomAmt = 10;


      var USA = new google.maps.LatLng(Lat, Lng);
      var mapOptions = {
        zoom: zoomAmt,
        center: USA,
        mapTypeId: google.maps.MapTypeId.TERRAIN
      };
      map =  new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    // Global var
       var infowindow = new google.maps.InfoWindow();

    //markers array
    var markers = [];

    //put all the markers on the map
    {% for location in locations %}

    //need to do the JSON encoding because JavaScript can't have Jinja2 variables
    //I need the safe here because Jinja2 tries to escape the characters otherwise
    var GPSlocation = {{location.GPSlocation|json_encode|safe}};    
    var LatLng = GPSlocation.replace("(", "").replace(")", "").split(", ")
    var Lat = parseFloat(LatLng[0]);
    var Lng = parseFloat(LatLng[1]);    

    var markerLatlng = new google.maps.LatLng(Lat, Lng);
    var title = {{location.title|json_encode|safe}}
    var iwContent = {{location.render_front()|json_encode|safe}}

    var marker = new google.maps.Marker({
            position: markerLatlng,
            title: title,
            map: map
      });

    google.maps.event.addListener(marker, 'click', function () {
      infowindow.setContent(iwContent);
      infowindow.open(map, marker);
      });

    //putting the marker onto the markers array
    markers.push(marker);


    {% endfor %}

    //creating the marker cluster
    var markerCluster = new MarkerClusterer(map, markers);

    }

    </script>
Run Code Online (Sandbox Code Playgroud)

就像我说的那样,在我打电话给MarkerClusterer后,地图看起来很正常.

geo*_*zip 6

您似乎需要从Marker中删除{map:map}属性.

这是一个集群的工作示例.

此页面上的 Chrome Javascript控制台中的错误:

  • 未捕获的ReferenceError:未定义GOverlay markerclusterer.js:630
  • 未捕获的ReferenceError:未定义标记

第一个暗示您使用了错误版本的markerclusterer脚本(GOverlay来自Google Maps API v2)

如果我将代码与正确的MarkerClusterer一起使用并声明markers数组,则clusterterer可以正常工作,但是InfoWindow内容与标记的关联存在问题(可以使用createMarker函数修复).

下面是一个使用createMarker函数来修复标记与infowindow的关联的示例.它基于您的代码,但仍有改进的余地(代码中存在大量冗余).