Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

googlemaps api MarkerClusterer problem

I am trying to use the map api MarkerClusterer feature with no luck:

var markersArray = [];

function getMarkers(hours) {//5

    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setMap(null);
        }
        markersArray.length = 0;
    }

    image = '/images/site/tw.png';

    $.ajax({
        url: "updateMarkers",
        type:"POST",
        data:{"hours": hours},
        success: function(data){
            data = $.parseJSON( data );
            if (data.Locations.length>0) {//2
                    for (i=0; i<data.Locations.length; i++) {//1
                        loc = new google.maps.LatLng(data.Locations[i].lat, data.Locations[i].lng);

                        marker = new google.maps.Marker({
                            position: loc,
                            map: map,
                            icon: image,
                            html: content
                        });

                        markersArray.push(marker);

                        infowindow = new google.maps.InfoWindow({
                            content: "holding..."
                        });

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

    var markerCluster = new MarkerClusterer(map, markersArray);

}//5

getMarkers(24);

I have looked at all the examples I can find and although I'm trying to do it within an ajax callback function I can see no other difference. I am getting the markers displaying normally on the map but no clustering effect.

like image 649
David Avatar asked Mar 18 '26 06:03

David


1 Answers

Ajax is async. What happen is you created the MarkerClusterer before the callback function is complete and thus no marker was pushed onto the global array markersArray. This is just on top of my head without any testing, but see if it solves the problem.

var markersArray = [], markerCluster;

function getMarkers(hours) {//5

    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setMap(null);
        }
        markersArray.length = 0;
    }

    image = '/images/site/tw.png';

    $.ajax({
        url: "updateMarkers",
        type:"POST",
        data:{"hours": hours},
        success: function(data){
            data = $.parseJSON( data );
            if (data.Locations.length>0) {//2
                    for (i=0; i<data.Locations.length; i++) {//1
                        loc = new google.maps.LatLng(data.Locations[i].lat, data.Locations[i].lng);

                        marker = new google.maps.Marker({
                            position: loc,
                            map: map,
                            icon: image,
                            html: content
                        });

                        markersArray.push(marker);

                        infowindow = new google.maps.InfoWindow({
                            content: "holding..."
                        });

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

                    //Create the Cluster AFTER all markers are pushed into the markersArray, and make sure it's called within the callback function
                    markerCluster = new MarkerClusterer(map, markersArray);
                }//2
            }
        });

}//5

getMarkers(24);
like image 100
KJYe.Name Avatar answered Mar 20 '26 20:03

KJYe.Name