iOS Mapbox annotation not opening

夙愿已清 提交于 2020-01-06 19:27:26

问题


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


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/39941588/ios-mapbox-annotation-not-opening

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!