Skip to content
Advertisement

Google Maps Marker Clustering not working

I got this code from this Snippet here that works fine and changed a little bit. But now I wanted to add Marker Clustering to the map like this but I get nothing to work. I added

JavaScript

to the head and

JavaScript

in front of “markers1 =” but then I get the error

JavaScript

I don’t know why this code is not working here without the marker cluster. On my Page, it works fine.

JavaScript
JavaScript
JavaScript

Advertisement

Answer

When I add the specified code to the posted code snippet:

  1. <script src="https://unpkg.com/@google/markerclustererplus@4.0.1/dist/markerclustererplus.min.js"></script> to the head
  2. var markerCluster = new MarkerClusterer(gmarkers1, {imagePath:'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'}); to after the gmarkers1 array is available (after the loop calling addMarker(markers1[i]);

I get the javascript error you report: Uncaught TypeError: e.getDraggable is not a function

That is because the constructor takes three arguments:

JavaScript

(your are missing the first map argument)

Update

two additional issues with the updated code in your question:

  1. the MarkerClusterer constructor is in the wrong place (it is outside the initialize function, it runs before the markers are created.
  2. the markers1 array is the wrong type to add to the MarkerClusterer, they need to be google.maps.Marker objects (the gmarkers1 array)

enter image description here

working code snippet:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement