I want to create MKPointAnnotaion's with the identifier number in the annotation overlay instead of the pin. So a 1 for the first annotation, a 2 for the second annotation, etc.
First, I added an MKPointAnnotation to the mapView based on a UILongPressGestureRecognizer.
With the mapView(_:viewFor:) I want to style those Annotations. This works to a certain extent but brings us to my problem:
I created a class CustomAnnotation to add an identifier to my MKPointAnnotaion:
class CustomAnnotation: MKPointAnnotation {
var identifier: Int!
}
And I set the identifier when registering UILongPressGesture:
let annotation = CustomAnnotation()
annotation.identifier = mapView.annotations.count
Then I use mapView(_:viewFor:) to change the appearance of my MKPointAnnotaion's. But I do not know how to set annotationView.glyphText so that it uses the identifier defined before for every created annotation individually.
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is CustomAnnotation else { return nil }
let annotationView = MKMarkerAnnotationView()
annotationView.glyphTintColor = .white
annotationView.glyphText = "44"
return annotationView
}
}
Thank you @Gerd Castan for your reference to the very extensive tutorial at kodeco
In the following I give a brief overview how I solved my problem - it might help someone in the future.
First of all I changed the type of my
CustomAnnotationclass from aMKPointAnnotationto anMKAnnotation. Now it is actually applicable to every Map Annotation use case.Then I created a class for the view representation of the
CustomAnnotationcalledCustomAnnotationView. In this case the type is anMKMarkerAnnotationViewbut you can also useMKAnnotationViewif you do not want the representation of the Annotations as markers.One annotation to the code cell above:
let alphabeticRepresentation = UnicodeScalar(number+1+64)!In my
UILongPressureGesturehandler I set theidentifier, so only the number ofCustomAnnotaion's are counted.In
viewDidload()of the MapViewController I registered theCustomAnnotationViewThat's it.