iOS Mapbox注释无法打开

时间:2016-10-09 08:33:47

标签: ios iphone swift3 mapbox

对于iOS开发人员来说相当新,我跟随iOS MapBox SDK的第一步" Swift教程(https://www.mapbox.com/help/first-steps-ios-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

2 个答案:

答案 0 :(得分:0)

此代码所在的位置

class ViewController: UIViewController{

它尚未配置为地图代理。该行应改为

class ViewController: UIViewController, MGLMapViewDelegate {

正如first steps guide

中所述

答案 1 :(得分:0)

除了在您的课程中添加MGLMapViewDelegate之外,如tmcw的答案中所述,您还必须在viewDidLoad()方法中添加以下内容:

    mapView.delegate = self