Why clusterAnnotationForMemberAnnotations in MKMapView is not called?
It appears as though it is required to set the clusteringIdentifier
for the MKAnnotationView. This has worked for me:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier, for: annotation)
annotationView.clusteringIdentifier = "identifier"
return annotationView
}
MapView clustering with custom annotations
Make sure that clusteringIdentifier
isn't unique for every cluster. Since you're passing (annotation as! StopAnnotation).id!
I'm guessing it is uniq.
From Apple documentation :
Clustering occurs when there is a collision between multiple annotation views with the same identifier on the map surface.
You can read more here.
How to check if annotation is clustered (MKMarkerAnnotationView and Cluster)
In iOS 11, Apple also introduce a new callback in MKMapViewDelegate:
func mapView(_ mapView: MKMapView, clusterAnnotationForMemberAnnotations memberAnnotations: [MKAnnotation]) -> MKClusterAnnotation
Before annotations become clustered, this function will be called to request a MKClusterAnnotation
for memberAnnotations
. So the second parameter named memberAnnotations
indicates the annotations to be clustered.
EDIT (4/1/2018):
There are two key steps for cluster annotation:
First, before annotations become clustered, MapKit invoke mapView:clusterAnnotationForMemberAnnotations:
function to request a MKClusterAnnotation for memberAnnotations
.
Secondly, MapKit add the MKClusterAnnotation to MKMapView, and mapView:viewForAnnotation:
function will be called to produce a MKAnnotationView.
So you can deselect annotation in either of the two steps, like this:
var selectedAnnotation: MKAnnotation? //the selected annotation
func mapView(_ mapView: MKMapView, clusterAnnotationForMemberAnnotations memberAnnotations: [MKAnnotation]) -> MKClusterAnnotation {
for annotation in memberAnnotations {
if annotation === selectedAnnotation {
mapView.deselectAnnotation(selectedAnnotation, animated: false)//Or remove the callout
}
}
//...
}
Or:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let clusterAnnotation = annotation as? MKClusterAnnotation {
for annotation in clusterAnnotation.memberAnnotations {
if annotation === selectedAnnotation {
mapView.deselectAnnotation(selectedAnnotation, animated: false)//Or remove the callout
}
}
}
//...
}
iOS 11 Cluster member
Clusters have a "memberAnnotations" property:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if #available(iOS 11.0, *) {
if let cluster = view.annotation as? MKClusterAnnotation {
//*** Need array list of annotation inside cluster here ***
let arrayList = cluster.memberAnnotations
// If you want the map to display the cluster members
mapView.showAnnotations(cluster.memberAnnotations, animated: true)
}
}
}
Annotations are hidden when zooming out
The current user's annotation doesn't have a cluster identifier. If you provide a cluster identifier for the current user's annotation, it works.
Related Topics
Cgaffinetransformmakerotation with Negative M_Pi
Convert Nsdate to String with a Specific Timezone in Swift
How to Set the Size of an Uiviewrepresentable
Outline Uilabel Text in Uilabel Subclass
Storing a Variable When the App Is First Installed
Turn Swift Object into a JSON String
How to Convert Bytes to Half-Floats in Swift
Impelementation of Rtcdatachannel of Webrtc in iOS
Swift: Sort Array by Sort Descriptors
Crashing While Collection View Cell Deletion Because of "Uicollectionviewlayoutattributes"
Save Video to a Custom Album Using Photos Framework in iOS
Scene Kit Memory Management Using Swift
Swift iOS Record Video and Audio with Avfoundation
How to Access Property or Method from a Variable
How to Create an Alert in a Swift File Model That Works for Various View Controller
How to Make Playground Execution Time Is as Fast as If We Run in iOS Application