Having problems adding markers into Google Maps clusterer
-
27-04-2021 - |
Question
I have problem adding markers into marker Clusterer of Google Maps. Firebug returns error:
Error: Invalid value for property : [object Object]' when calling method: [nsIDOMEventListener::handleEvent]
Markers are created with mix of JavaScript and PHP:
// loop starts here.......
var randLatLng = new google.maps.LatLng( <?php echo $lat; ?>, <?php echo $lon; ?> );
var marker_<?php echo $gauging["Gauging"]["id"]; ?> = new google.maps.Marker({
map: MyMap.map,
title: '<?php echo $gauging["Gauging"]["identification"]; ?>',
position: randLatLng,
draggable: false,
clickable: true,
icon: '/img/markers/yellow_Marker.png',
myId: 'gp_<?php echo $gauging["Gauging"]["id"]; ?>'
});
myMap.markers.push(marker_<?php echo $gauging["Gauging"]["id"]; ?>);
// loop ends here .......
var markerYellowCluster = new MarkerClusterer(myMap, yellowMarkers);
... and this code creates all of the markers fine, but it does not add them into clusterer.
Can you give me some suggestions how I can fix this?
Tnx in adv!
UPDATE: maybe this can help - alert(yellowMarkers); shows alert window, with:
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Solution
When I've done this before, my code looked like this:
var markerCluster = new MarkerClusterer(map, markers, {
zoomOnClick: true,
averageCenter: true
});
What is yellowMarkers
? Shouldn't that be myMap.markers?
OTHER TIPS
I agree with some of the comments above about what your variables are named, but I wanted to add another option here.
You can initialize the markerclusterer before you start adding any markers:
var markerYellowCluster = new MarkerClusterer(myMap);
Then instead of calling:
myMap.markers.push(marker_<?php echo $gauging["Gauging"]["id"]; ?>);
You can simply call the "addMarker" method on the markerclusterer object itself and it will both add the marker to the map as well as the marker clusterer:
markerYellowCluster.addMarker(marker_<?php echo $gauging["Gauging"]["id"]; ?>);