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.
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With