Can't remove all Google maps markers

Hi there !

I’ve got some issue trying to remove all my google maps markers when doing some actions.

So, i have 3 types of markers on my map : markers for friends, a marker for my position and markers for points of interest.

I need to see my position anytime, so my_position marker has a specific title => “my_position”.
But when I need to remove my points of interest markers or my friends markers, i can’t remove them all doing this:

for (let i = 0; i < this.markers.length; i++) {
      if(this.markers[i].title !== 'my_position') {
          this.markers[i].setMap(null);
          this.markers.splice(i, 1);
      }
}

For the friends markers, sometimes it works, sometimes it doesn’t.
And for the points of interest markers (there is 3 of them), one of them is always there and i don’t know how to remove it.

If you need more code or have any questions, feel free to ask.
Thanks !

when you splice, you modify the length of your tab, the 'i+1’est element becomes 'i’est, and so your loop misses elements

2 Likes

Yeah that was it, I figured it out yesterday but had no time to close this thread !
Thanks anyway for the help though, appreciate :wink:

Same thing happen with me.