Mapbox iOS SDK 3 - 在点击注释时不显示注释标题

时间:2016-01-16 16:37:51

标签: ios xcode swift annotations mapbox

当用户点击指向当前位置的注释时,我很难弄清楚如何使注释标题出现。除此之外,一切都很好。我正在使用https://github.com/varshylmobile/LocationManager中的CLLocationManager包装,但我认为它不会影响Mapbox,因为我可以在地图上正确显示我的引脚。

以下是截图示例:

Mapbox with 2 annotations

正如您所看到的,我可以毫无问题地运行应用程序。就在我为引脚设置一些注释标题和副标题,并在运行时点击它时,它没有显示任何内容。

这是注释的代码片段:

        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)

这是呼出功能:

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

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

我在iOS 9.2,xCode 7上。

谢谢!

2 个答案:

答案 0 :(得分:3)

我在实例化地图视图后忘记了这行代码。

 mapView.delegate = self

谢谢!

答案 1 :(得分:2)

Here's an example如何使用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()
    }
}