Question

I want to use MarkerClusterer on my map but I do not know very well JavaScript.

How to use MarkerClusterer with my map?

i need to add the cluster command but I do not know very well coding. my code is below

var LocationData = [ 

     [49.2814064, -123.1025187, "71 E Hastings St, Vancouver" ], 
     [49.2812336, -123.1020622, "122 E Hastings St, Vancouver" ], 
     [49.2813564, -123.1012253, "138 E Hastings St, Vancouver" ], 
    [49.2811625, -123.0985032, "242 E Hastings St, Vancouver" ]
];

function initialize() {
    var map =
        new google.maps.Map(document.getElementById('map_canvas'));
    var bounds = new google.maps.LatLngBounds();
    var infowindow = new google.maps.InfoWindow();

    for (var i in LocationData) {
        var p = LocationData[i];
        var latlng = new google.maps.LatLng(p[0], p[1]);
        bounds.extend(latlng);

        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            title: p[2]
        });

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

    map.fitBounds(bounds);
}


google.maps.event.addDomListener(window, 'load', initialize);
google.maps.event.addListener(marker, 'click', function () {
    infowindow.setContent(p[2]);
    infowindow.open(map, marker);
});
Was it helpful?

Solution

You will have to add a link to markerclusterer library like:

<script src='markerclusterer.js'></script>

One possible solution is to build an array of markers and add that array at once to cluster. Like:

function initialize() {
    var map =
        new google.maps.Map(document.getElementById('map_canvas'));
    var bounds = new google.maps.LatLngBounds();
    var infowindow = new google.maps.InfoWindow();

    var markers = [];
    var markerClusterer;

    for (var i in LocationData) {
        var p = LocationData[i];
        var latlng = new google.maps.LatLng(p[0], p[1]);
        bounds.extend(latlng);

        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            title: p[2]
        });

        markers.push(marker);

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

    map.fitBounds(bounds);

    markerClusterer = new MarkerClusterer(map, markers);
}

and then zoom out to get cluster visible.

OTHER TIPS

In the loop where you're creating markers, you'll want to store each one you create in an array, then pass a reference to your map and that array to the clustering constuctor:

var markers = [],
    i, l
    marker,
    markerCluster;

for (i = 0, l = LocationData.length; i < l; i += 1) {
    ...
    marker = new google.maps.Marker({ // whatever you need to pass in here });
    markers.push(marker);
}
markerCluster = new MarkerClusterer(map, markers);  

also, avoid using for/in for looping through arrays, use just a regular for loop

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top