0

I'm displaying custom annotations over the map and having hard times to receive didSelect calls on my delegate. Here is the code of the ViewController:

class TestAnnotationClickViewController: UIViewController, MGLMapViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        let mapView = MGLMapView(frame: view.bounds)
        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        mapView.delegate = self

        mapView.addAnnotation(TestAnnotation())

        view.addSubview(mapView)
    }

    func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
        if annotation is TestAnnotation {
            let view = TestAnnotationView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
            return view
        }
        return nil
    }

    func mapView(_ mapView: MGLMapView, didSelect annotation: MGLAnnotation) {
        print("annotation didSelect")
    }

    func mapView(_ mapView: MGLMapView, didSelect annotationView: MGLAnnotationView) {
        print("annotation view didSelect")
    }
}

Here is the code for annotation class and corresponding view:

class TestAnnotation: NSObject, MGLAnnotation {

    var coordinate: CLLocationCoordinate2D

    override init() {
        coordinate = CLLocationCoordinate2D(latitude: 33.9415889, longitude: -118.4107187)
    }
}

class TestAnnotationView: MGLAnnotationView {

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupView()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    private func setupView() {
        backgroundColor = .green
    }
}

When I'm pressing the annotation (green rectangle) I expect delegate methods didSelect to be called. However neither of them is invoked. And the console doesn't get "annotation didSelect" or "annotation view didSelect" printed.

I also tried to set isUserInteractionEnabled on the TestAnnotationView but it didn't help. What am I missing?

I install Mapbox (5.9.0) via cocoapods:

pod 'Mapbox-iOS-SDK', '~> 5.9'
RustamG
  • 1,815
  • 1
  • 14
  • 16

1 Answers1

0

I tend to use reuseIdentifiers for the creation of annotations and construct an init that carries that and the annotation through so for your use-case, something like:

func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
    if annotation is TestAnnotation {
        let view = TestAnnotationView(reuseIdentifier: "test", frame: CGRect(x: 0, y: 0, width: 100, height: 100), annotation: annotation)
        return view
    }
    return nil
}

and in the TestAnnotationViewClass adding the initialiser:

init(reuseIdentifier: String?, frame: CGRect, annotation: MGLAnnotation) {
    super.init(reuseIdentifier: reuseIdentifier)

    self.frame = frame
    setupView()
}

ensures that everything is setup so the annotations can respond to touches and trigger the didSelect delegate methods.

Magnas
  • 3,832
  • 5
  • 33
  • 48
  • Thanks @Magnas. This kind of makes it working. However I'm having different issues with this. Here is a repo that demonstrates the issues I'm experiencing: https://github.com/RustamG/mapbox-ios-issue-demo. And a video if you are interested: https://github.com/RustamG/mapbox-ios-issue-demo/raw/master/Video.mp4 (17MB) – RustamG Jun 11 '20 at 14:26