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'