Getting Google Maps API 3 MapClusterer to work with my code?

409 views Asked by At

Please help! I have way too many pins, and it's growing!

Here is the code I am using to places pins on a map at www.findmyfields.com THIS CODE WORKS (As you can see on the website).

The non-working version is at 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>

I am having a hard time getting the Map Clusterer to work.

When I try to add the clustering code (See below)

// 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);

}

The whole site locks up and never loads.

1

There are 1 answers

0
ermoore32 On

Okay, so after days of testing, I finally figured out my issue, and it's kinda funny how simple it was. Seems like someone else would have noticed it for me though.

Basically, I had to create a SEPARATE variable/array for the push.(marker) to send to the clusterer since I was already using var markers [] for the pins.

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

    ];

Then push markers to new array...

        clustermarkers.push(marker);

And finally, display the array ...

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

now www.findmyfields.com is A LOT cleaner looking.