【问题标题】:iOS Mapbox annotation not openingiOS Mapbox注释未打开
【发布时间】:2017-02-17 21:21:47
【问题描述】:

对于 iOS 开发人员来说相当新,我正在学习 Swift (https://www.mapbox.com/help/first-steps-ios-sdk/) 的“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

【问题讨论】:

标签: ios iphone swift3 mapbox


【解决方案1】:

这段代码说明的地方

class ViewController: UIViewController{

它尚未配置为地图的委托。这一行应该是

class ViewController: UIViewController, MGLMapViewDelegate {

first steps guide 中指定的那样。

【讨论】:

  • 啊,谢谢!必须调整 Plus 函数而不是 MGLPointAnnotation 它必须是 MGLAnnotation 在:func mapView(_ mapView: MGLMapView, viewFor: MGLAnnotation) -> MGLAnnotationView?{ return nil }
【解决方案2】:

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

    mapView.delegate = self

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多