问题
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