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

大城市里の小女人 提交于 2019-12-11 15:46:50

问题


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?


回答1:


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

e.g.

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



回答2:


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.



来源:https://stackoverflow.com/questions/51010956/how-can-i-know-if-an-annotation-is-already-on-the-mapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!