获取Google Maps API 3 MapClusterer可以使用我的代码吗?

时间:2015-06-17 17:01:23

标签: google-maps-api-3 markerclusterer

请帮忙!我的针脚太多了,而且它还在增长!

以下是我用于在地图上放置引脚的代码www.findmyfields.com此代码工作(如您在网站上看到的那样)。

非工作版本位于http://reddirtsoftball.com/map/maptest.php

    <script>
function initialize() {

    var map;
    var bounds = new google.maps.LatLngBounds();
    var mapOptions = {
        mapTypeId: 'roadmap'
    };

    // Display a map on the page
    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
    map.setTilt(45);

    // Multiple Markers
    var markers = [

            //I have some php in here with a while loop that gets the info for the markers.

    ];

    // Info Window Content
    var infoWindowContent = [


            //I have some php in here with a while loop that gets the info for the window content.

    ];

    // Display multiple markers on a map
    var infoWindow = new google.maps.InfoWindow(), marker, i;

    // Loop through our array of markers & place each one on the map  
    for( i = 0; i < markers.length; i++ ) {
        var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
        bounds.extend(position);
        marker = new google.maps.Marker({
            position: position,
            map: map,
            title: markers[i][0]
        });

        // Allow each marker to have an info window    
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infoWindow.setContent(infoWindowContent[i][0]);
                infoWindow.open(map, marker);
            }
        })(marker, i));

        // Automatically center the map fitting all markers on the screen
        map.fitBounds(bounds);
    }

    // Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
    var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
        this.setZoom(4);
        google.maps.event.removeListener(boundsListener);
    });

}

google.maps.event.addDomListener(window, 'load', initialize);
</script>

我很难让Map Clusterer工作。

当我尝试添加聚类代码时(见下文)

// Allow each marker to have an info window    
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infoWindow.setContent(infoWindowContent[i][0]);
            infoWindow.open(map, marker);
        }
    })(marker, i));
    markers.push(marker);

    // Automatically center the map fitting all markers on the screen
    map.fitBounds(bounds);

        // Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
    this.setZoom(4);
    google.maps.event.removeListener(boundsListener);
});

 var markerCluster = new MarkerClusterer(map, markers);

}

整个网站锁定并且从不加载。

1 个答案:

答案 0 :(得分:0)

好的,经过几天的测试后,我终于弄明白了我的问题,它有点有趣,多么简单。看起来像其他人会注意到它对我来说。

基本上,我必须为push。(marker)创建一个SEPARATE变量/数组,以便发送到clusterer,因为我已经在使用var markers []作为引脚。

// Multiple Markers
    var clustermarkers = [];
    var mcOptions = {gridSize: 25};
    var markers = [
    <?
   $x = 0;
        while ($x < $i){
        echo '["' . $fieldname[$x] .'",' . $fieldlat[$x] .', ' . $fieldlong[$x] .' ],'; 
        $x++;
        }
        ?>

    ];

然后将标记推送到新阵列...

        clustermarkers.push(marker);

最后,显示数组......

var markerCluster = new MarkerClusterer(map, clustermarkers, mcOptions);

现在www.findmyfields.com看起来很干净。