Enhance the readability of a map by replacing overlapping annotations with a clustering annotation view.
- iOS 11.0+
- Xcode 10.0+
TANDm is a fictional bike sharing app that uses annotation clustering to provide an uncluttered map. The app shows how MapKit automatically groups two or more annotations into a single annotation when spacing on the map doesn’t permit each annotation to be visible without overlapping. This enhances the readability of the map by replacing overlapping annotations with a clustering annotation view.
To group annotations into a cluster, set the
clustering property to the same value on each annotation view in the group. For example, to show overlapping unicycle annotations in a clustering annotation view, TANDm sets
clustering on each instance of
To determine how an annotation view behaves when it overlaps another annotation view, set its
display property. In the sample app, the map view is likely to hide the unicycle annotation if it overlaps with another annotation because the unicycle annotation view has a display priority of
default, while the display priorities for bicycle and tricycle are set to
default. Here’s an example of setting the display priority while preparing an instance of
Bicycle for reuse:
Custom Clustering Annotation Views
Customize the behavior and appearance of a clustering annotation view by subclassing
MKAnnotation; for instance, to display hints about the annotations within the cluster. TANDm, for example, uses the custom clustering annotation view
Cluster to show the ratio between bicycles and tricycles at a location.