0
votes

Fairly new to iOS dev and I'm following the "first steps with the iOS MapBox SDK" tutorial for Swift (https://www.mapbox.com/help/first-steps-ios-sdk/). Things were fine with displaying the map, custom style, and adding an annotation. But then encountered an issue when trying to interact with the annotation to display its title and subtitle.

The print("allow") print("tap on callout") don't seem to ever get called. Even mapView.selectAnnotation(point, animated: true) doesn't do anything (and it wasn't doing anything without it either)

Using xCode 8.0, MacOS Sierra, Swift 3, iOS 10

I'm a bit clueless now, any advice would be appreciated

Thanks in advance

ViewController.swift:

import UIKit
import Mapbox
class ViewController: UIViewController{

    @IBOutlet var mapView: MGLMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let point = MGLPointAnnotation()
        point.coordinate = CLLocationCoordinate2D(latitude: 46.082666, longitude:7.508510)
        point.title = "Title"
        point.subtitle = "Subtitle"
        mapView.addAnnotation(point)
        mapView.selectAnnotation(point, animated: true)
        print("add annotation");

    }

    // Return `nil` here to use the default marker.
    func mapView(mapView: MGLMapView, viewForAnnotation annotation: MGLPointAnnotation) -> MGLAnnotationView? {
        print("default marker")
        return nil
    }

    // Allow callout view to appear when an annotation is tapped.
    func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLPointAnnotation) -> Bool {
        print("allow")
        return true
    }
    func mapView(mapView: MGLMapView, tapOnCalloutForAnnotation annotation: MGLAnnotation) {
        print("tap on callout")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Screenshot: Annotation displays where it's supposed to but cannot be interacted with

2
Is the mapView delegate set up?Sealos
Your tags already indicate you are using Swift. There is no need to add that to the title.Leo Natan

2 Answers

0
votes

Where this code states

class ViewController: UIViewController{

It hasn't been configured to be the map's delegate. This line should instead be

class ViewController: UIViewController, MGLMapViewDelegate {

As specified in the first steps guide.

0
votes

Aside from adding MGLMapViewDelegate to your class, as stated in tmcw's answer, you also have to add the following to your viewDidLoad() method:

    mapView.delegate = self