Sam
Sam

Reputation: 41

Mapbox iOS SDK 3 - annotation title not showing when annotation is tapped

I'm having a hard time figuring out how to make the annotation title appear when a user tapped an annotation pointed to the current location. Everything is working fine except this. I am using CLLocationManager wrapper from https://github.com/varshylmobile/LocationManager but I don't think it's affecting the Mapbox since I can display my pins correctly on the map.

Here's the screenshot sample:

Mapbox with 2 annotations

As you can see, I could run the app without any problems. It's just when I set some annotation title and subtitle to the pin, and tapped it at run-time, it doesn't show anything.

Here's my code snippet for the annotation:

        let point = MGLPointAnnotation()
        point.coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
        point.title = "Hello world!"
        point.subtitle = "Welcome to The Ellipse."

        self.mapView.addAnnotation(point)

        let point2 = MGLPointAnnotation()
        point2.coordinate = CLLocationCoordinate2D(latitude: 37.43259552, longitude: location.coordinate.longitude)
        point2.title = "Hello world!"
        point2.subtitle = "Welcome to The Ellipse."

        self.mapView.addAnnotation(point2)

        self.mapView.selectAnnotation(point, animated: true)
        self.mapView.showAnnotations([point, point2], animated: true)

and here is the call out function:

func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
    return true
}

func mapView(mapView: MGLMapView, imageForAnnotation annotation: MGLAnnotation) -> MGLAnnotationImage? {
    return nil
}

I am on iOS 9.2, xCode 7.

Thank you!

Upvotes: 1

Views: 1527

Answers (2)

friedbunny
friedbunny

Reputation: 2421

Here's an example of how to configure basic callout views with the Mapbox iOS SDK:

import Mapbox

class ViewController: UIViewController, MGLMapViewDelegate {

    var mapView: MGLMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        mapView = MGLMapView(frame: view.bounds)
        mapView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

        view.addSubview(mapView)

        // remember to set the delegate (or much of this will not work)
        mapView.delegate = self

        addAnnotation()
    }

    func addAnnotation() {
        let annotation = MGLPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2DMake(35.03946, 135.72956)
        annotation.title = "Kinkaku-ji"
        annotation.subtitle = "\(annotation.coordinate.latitude), \(annotation.coordinate.longitude)"

        mapView.addAnnotation(annotation)

        // fit the map to the annotation(s)
        mapView.showAnnotations(mapView.annotations!, animated: false)

        // pop-up the callout view
        mapView.selectAnnotation(annotation, animated: true)
    }

    func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
        return true
    }

    func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? {
        if (annotation.title! == "Kinkaku-ji") {
            let label = UILabel(frame: CGRectMake(0, 0, 60, 50))
            label.textAlignment = .Right
            label.textColor = UIColor(red: 0.81, green: 0.71, blue: 0.23, alpha: 1)
            label.text = "金閣寺"

            return label
        }

        return nil
    }

    func mapView(mapView: MGLMapView, rightCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? {
        return UIButton(type: .DetailDisclosure)
    }

    func mapView(mapView: MGLMapView, annotation: MGLAnnotation, calloutAccessoryControlTapped control: UIControl) {
        // hide the callout view
        mapView.deselectAnnotation(annotation, animated: false)

        UIAlertView(title: annotation.title!!, message: "A lovely (if touristy) place.", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "OK").show()
    }
}

Upvotes: 3

Sam
Sam

Reputation: 41

I forgot this line of code after instantiating my map view.

 mapView.delegate = self

Thanks!

Upvotes: 3

Related Questions