This doesn't work because markers
array is empty at line
var markerCluster = new MarkerClusterer(map, markers);
because geocoder.geocode()
is asynchronous and needs some time to finish. One "ugly" fix could be just to provide some time that all markers are properly set up. For example:
setTimeout(function() {
var markerCluster = new MarkerClusterer(map, markers);
}, 500);
And, maybe you will have to lower zoom level to see clustering.
The other solution is to change markerCluster
to global variable, move it before for
loop:
markerCluster = new MarkerClusterer(map, markers);
for (var i = 0; i < listNumber.length; i++) {
...
and change function addMarkers()
so that instead of
markers.push(marker);
you call
markerCluster.addMarker(marker);
See example at jsBin.