Swift obtain visible annotations/clusters shown in map

纵然是瞬间 提交于 2019-12-11 18:14:17

问题


I'm currently developing an app where I load user images as an annotation onto a map. Currently, I use an API to load images in viewForAnnotation for each image annotation and if the user has more than a few thousand photos, this image loading function is called every time and degrades performance significantly.

Is there anyway I can obtain just the annotations which are shown on the map currently, i.e. only the annotations and clusters of annotations that are visible to the user? Apple's API for annotations(in:) using visibleMapRect returns basically every single annotation whereas I just want the high-level or currently displaying annotation. For example, in the image linked below, there are 2 annotations appearing but these 2 annotations are clusters of hundreds of annotations. Is there any way to just load image for these two instead of every single annotation?


回答1:


You can do you check like this:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKClusterAnnotation {
        // Need to load images
    }
    ...
}


来源:https://stackoverflow.com/questions/51144550/swift-obtain-visible-annotations-clusters-shown-in-map

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