1

I have a array containing all the markers of the map. I have no trouble placing them on the map even using MarkerClusterer with them. addListener for the MarkerClusterer works just fine, but I just simply can't get marker addListener to trigger the event. Here is the actual code:

function initializeMap(markerArray) {
var latlng = new google.maps.LatLng(0.0, 0.0);
var mapOptions = {
    zoom: 2,
    maxZoom: 21,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

var length = markerArray.length,
    element = null;
for (var i = 0; i < length; i = i + 2) {
    var latlng = new google.maps.LatLng(markerArray[i],markerArray[i+1]);
    var marker = new google.maps.Marker({
        map: map,
        position: latlng,
        title:'Click to zoom'
    });
    markers.push(marker);
}

google.maps.event.addListener(marker, 'onclick', function() {
    alert("I have been clicked");
});

var markerCluster = new MarkerClusterer(map, markers);

google.maps.event.addListener(markerCluster, 'clusterclick', function(cluster) {
    alert("I have been clicked");
});

}

1 Answer 1

3

You only have a click listener on your last marker (at least in the code you posted).

working markerclusterer example

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, thanks geocodezip! I had a completely wrong understanding of how addListener works. I just put all the markers in array and now it works! Thanks a lot!
is there any way to add zoom on click, now it is not zooming to the location

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.