Mr beaver
Mr beaver

Reputation: 13

How can I know if an annotation is already on the mapview

I trying to add annotation pin to Mapview if and only if there is no such same pin already on mapview to avoid having multiple repeat annotations at the same location.

Any one can show me help?

Upvotes: 0

Views: 1227

Answers (2)

TheAppMentor
TheAppMentor

Reputation: 1089

The approach suggested by @Kosuke Ogawa would work. However it is not a good idea to rely on the UI determine application state.

For eg: You most probably have a list of annotations stored in some sort of data structure in your view controller (I am guessing an Array). This data structure should be the source of truth.

In your case, to determine if a an Annotation is already on the map, check if the Data structure that feeds the mapview contains the annotation and proceed.

Upvotes: -1

Kosuke Ogawa
Kosuke Ogawa

Reputation: 7451

You can use mapView.view(for:) method.

e.g.

    if (self.mapView.view(for: annotation) != nil) {
        print("pin already on mapview")
    }

Upvotes: 1

Related Questions