I need to display custom annotations that have downward-pointing pushpin that looks like this:
So I inherited MGLAnnotationView
and set centerOffset
in layoutSubviews()
:
class ZoneMapAnnotationView: MGLAnnotationView {
// ...
// code for view initialization is omitted
// ...
override func layoutSubviews() {
super.layoutSubviews()
centerOffset = CGVector(dx: bounds.width / 2, dy: -bounds.height / 2)
}
}
As a result I see parts of annotation views at the top left corner that are not in currently visible map area:
Here is how I instantiate the views:
extension ZonesMapWrappedView: MGLMapViewDelegate {
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
guard let annotation = annotation as? ZoneMapAnnotation else {
return nil
}
let reuseIdentifier = "ZoneMapAnnotation"
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? ZoneMapAnnotationView
?? ZoneMapAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
annotationView.bubbleColor = annotation.bubbleColor
annotationView.text = annotation.title ?? ""
return annotationView
}
}
The issue doesn't appear if I create the annotation view either without providing reuseIdentifier
or making it unique for each annotation. But doing so doesn't make sense to me. Because I thought that reuseIdentifier
is supposed to identify annotation appearance type.
Full code
Here is a repo that demonstrates the issues I'm experiencing.