Swift中的MapBox - 允许标记的注释具有链接到视图的按钮

时间:2017-01-30 20:21:48

标签: ios swift mapbox mapbox-marker

我已经设置了一个带有选项卡式地图视图的简单应用程序(选项卡1上的地图),该应用程序使用SwiftyJSON和Alamofire从服务器上的json文件(点信息存储在MySQL中)读取信息。这些点被加载到地图中。我想要做的是在注释中添加一个按钮,该按钮会连接到不同的视图选项卡并传递到该点的ID。

我还没有尝试任何东西,因为我不知道从哪里开始,文档没有提到类似的东西。

如何为地图点注释添加插座?

2 个答案:

答案 0 :(得分:1)

点按您的图钉时,标注会显示标题和/或副标题。您需要在此标注中添加左右标注附件视图。您可以通过遵循MGLMapViewDelegate并实现以下方法来实现此目的:

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

func mapView(_ mapView: MGLMapView, leftCalloutAccessoryViewFor annotation: MGLAnnotation) -> UIView? {
    ...your code here...
    let deleteButton = UIButton(type: .custom)
    deleteButton.tag = 100
}

func mapView(_ mapView: MGLMapView, rightCalloutAccessoryViewFor annotation: MGLAnnotation) -> UIView? {
    ...your code here...
    let infoButton = UIButton(type: .custom)
    infoButton.tag = 101
}

func mapView(_ mapView: MGLMapView, annotation: MGLAnnotation, calloutAccessoryControlTapped control: UIControl) {
    ...your code here...
    switch control.tag {
    case 100:
       // do some delete stuff
    case 101:
       // do some info stuff
    default:
       break 
}

显示实际用法的MapBox示例如下: MapBox example。这个例子不会偏离,但你只需用按钮点击而不是UIAlert来触发segue。

答案 1 :(得分:1)

供将来参考:

func mapView(_ mapView: MGLMapView, annotation: MGLAnnotation, calloutAccessoryControlTapped control: UIControl) {
    // Hide the callout view.
    // mapView.deselectAnnotation(annotation, animated: true)

    performSegue(withIdentifier: "toDetail", sender: view)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    print("Seque toDetail")
    // do stuff
}