iOS Mapbox 注释打不开

iOS Mapbox annotation not opening

对 iOS 开发人员相当陌生,我正在学习 Swift (https://www.mapbox.com/help/first-steps-ios-sdk/) 的 "first steps with the iOS MapBox SDK" 教程。 显示地图、自定义样式和添加注释都很好。但是在尝试与注释交互以显示其标题和副标题时遇到问题。

print("allow") print("tap on callout") 似乎从来没有被调用过。甚至 mapView.selectAnnotation(point, animated: true) 也没有做任何事情(没有它它也没有做任何事情)

使用 xCode 8.0,MacOS Sierra,Swift 3,iOS 10

我现在有点无能,任何建议将不胜感激

提前致谢

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

此代码说明的地方

class ViewController: UIViewController{

尚未配置为地图的代理。这一行应该改为

class ViewController: UIViewController, MGLMapViewDelegate {

first steps guide中所指定。

除了将 MGLMapViewDelegate 添加到您的 class 之外,如 tmcw 的回答所述,您还必须将以下内容添加到您的 viewDidLoad() 方法:

    mapView.delegate = self