2

I need to display custom annotations that have downward-pointing pushpin that looks like this:

marker

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:

markers in top left corner

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.

RustamG
  • 1,815
  • 1
  • 14
  • 16
  • I looked at your repo and I think the problem lies somewhere inside your ZoneMapAnnotationView code. As you say, if you replace your `let annotationView = mapView.dequeueReusable...` with a simple `let annotationView = ZoneMapAnnotationView()` the problem disappears on my simulator too. I'm afraid the actual culprit is beyond me. – Magnas Jun 11 '20 at 16:17
  • @Magnas After more testing it turned out that under some conditions `let annotationView = ZoneMapAnnotationView()` doesn't help. The issue appears not right away but after some map panning and zooming. Anyway thanks for your attention. – RustamG Jun 11 '20 at 16:53
  • 1
    I saw a similar issue, and found that removing any Auto Layout code from my `MGLAnnotationView` fixed it (i.e., use old-fashioned manual layout instead). Not ideal, but it worked for me. – musical_coder Sep 11 '20 at 17:36
  • @musical_coder Yes. I refactored custom annotation views at some point to simplify the layout structure. This involved replacing UILabel with manual drawing (CoreGraphics) and some other things. Not sure what exactly helped but seems like reducing Auto Layout stuff did the trick. – RustamG Sep 14 '20 at 06:44

0 Answers0