user781439
user781439

Reputation: 257

Google V3 - Referencing markers

I have a script that adds markers one by one to a map

var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(51,-117)
                });
marker.setIcon(getIconFile(initialIconId));
markers.push(new Array(id,marker)); // id, marker
marker.setMap(map);

later on in the script when I press a custom button I want to be able to change the markers icons. So I grab the marker by the id from the markers array and call:

 markers[markerIndex].setIcon(getIconFile(newIconId)); // which returns a string url of the icon

However I receive: TypeError: markers[markerId].setIcon is not a function

I print the markerId and it is valid, I also print the result of indexing the marker markers[markerId] and it returns a marker object. I have no other way to debug this I am lost!

Thanks

Upvotes: 1

Views: 1065

Answers (1)

mihai
mihai

Reputation: 38543

It seems like you're pushing an Array into the markers, instead of just one element.

Why not:

markers = []
markers.push(marker)
markers[markerIndex].setIcon(getIconFile(newIconId));

or if you insist inserting an array:

markers.push(new Array(id,marker));
markers[markerIndex][1].setIcon(getIconFile(newIconId));

Upvotes: 2

Related Questions