The object does not have a getPosition method when clustering Google map markers (api v3)

I am trying to do a basic implementation of clustering tokens on a Google map using Google Maps Utility Library v3 .

When I run this, I get an error in the Chrome Developer Tools console:

Uncaught TypeError: Object #<Object> has no method 'getPosition' 

This refers to line 649 in the script utility library here: http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer.js . This is the following function:

 /** * Determins if a marker is contained in a bounds. * * @param {google.maps.Marker} marker The marker to check. * @param {google.maps.LatLngBounds} bounds The bounds to check against. * @return {boolean} True if the marker is in the bounds. * @private */ MarkerClusterer.prototype.isMarkerInBounds_ = function(marker, bounds) { return bounds.contains(marker.getPosition()); }; 

The code I use is standard Google maps material, the main function of which is:

 function initialize(items,loop,zoom) { geocoder = new google.maps.Geocoder(); if (items.length > 0) { var latlng = new google.maps.LatLng(items[0].Lat, items[0].Lng); var myOptions = { zoom: zoom, center: latlng, //mapTypeControl: false, streetViewControl: false, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map"), myOptions); map.setOptions({styles: stylez}); for (var i = 0; i < items.length; i++) { var marker = new google.maps.Marker({ position: new google.maps.LatLng(items[i].Lat, items[i].Lng), title: items[i].Title, icon: _iconCenter, shadow: shadow, infocontent: items[i].Description }); marker.setMap(map); markersArray.push(marker); } var markerCluster = new MarkerClusterer(map, items); google.maps.event.addListener(map, "tilesloaded", function () { if(loop == true){ SetLoop(); } }); } } 

I traced the error function as far as I understand, and it should get the coordinates for the edges of the map in order to be able to determine the boundaries, which should only be standard behavior, but clearly something is wrong.

I wondered if anyone could shed some light on this?

Thanks for any pointers ...

+6
source share
2 answers

The problem turned out to be that I was declaring a markerclusterer script before the Google Maps script. The calling card script first solved this ... now it is obvious!

+4
source

MarkerClusterer expects an array of markers. You create one, but pass an array of elements to its constructor. Change:

 var markerCluster = new MarkerClusterer(map, items); 

in

 var markerCluster = new MarkerClusterer(map, markersArray); 
+4
source

Source: https://habr.com/ru/post/922931/


All Articles