Question

I've trying to implement the marker cluster for hours but somehow I don't know why my map still only show the markers instead of cluster. I've been searching all the questions and tried to implement it but still without any luck. Can anyone tell me where I did wrong? Thank you so much!

            function initialize() {
                    var mapOptions = {
                    center: new google.maps.LatLng(38.822591,152.871087),
                    zoom: 2,
                    mapTypeControlOptions: {
                        mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
                    }

                };
                var map = new google.maps.Map(document.getElementById('map'),
                mapOptions);

                var locations = <?php echo json_encode($fLocations) ?>;


              var gmarkers = [];
              for (var i = 0; i < locations.length; i++) {
                     var beach = locations[i];
                    var myLatLng = new google.maps.LatLng(beach['latitude'], beach['longitude']);
                    var marker = new google.maps.Marker({
                        position: myLatLng,
                        map: map
                    });
             gmarkers.push(marker);
              }
              var markerCluster = new MarkerClusterer(map, gmarkers);
            }

            google.maps.event.addDomListener(window, 'load', initialize);
Was it helpful?

Solution

Try not setting the map option for each marker, so

var marker = new google.maps.Marker({
                    position: myLatLng,
                    map: map
                });

Becomes:

var marker = new google.maps.Marker({position: myLatLng});

Setting the map will automatically push the marker onto it.

Does this also instantiate the marker clusters now?

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