Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps - How to locate a marker in a markers array?

How can I check if a google map marker is already inside an array of markers?
Even after this markersArray.push(marker); the condition (marker in markersArray) is false.

like image 457
Ash Avatar asked Jun 01 '11 06:06

Ash


2 Answers

First, (marker in markersArray) is wrong since in doesn't look for elements in the array.
It looks for properties.

The way it worked for me was

for (var i=0; i<markersArray.length; i++) {
    if (markersArray[i].getPosition().equals(marker.getPosition())) {
...

This works as long as what you need compared is only the coordinates of the markers.
We use here the LatLng class' .equals operator.

like image 155
Ash Avatar answered Oct 18 '22 23:10

Ash


You should iterate through the array to check for the marker.

for (var i=0; i<markersArray.length; i++) {
  if (markersArray[i] === marker) {
     //doSomething...
     break;
     }
  }
like image 42
rob Avatar answered Oct 18 '22 23:10

rob