当我点击MapKit的引脚时,如何执行操作?ios9,swift 2

sdo*_*ble 7 mapkit ios swift

我已经创建了一个地图上有大量针脚的地图,然后点击一个针脚会弹出默认的"气泡",这就好了.

我真正想做的不是弹出泡泡而是调用不同的函数.我的所有搜索都导致人们想要创建具有不同视图的新自定义注释,这样,我只想调用一个函数,我不确定在哪里尝试调用它.我对ios开发很新,这看起来应该很简单,但我发现通常情况并非如此.

Ron*_*ara 8

您可以通过将canShowCallout属性设置为on来停止显示标注气泡MKAnnotationView

anView!.canShowCallout = false
Run Code Online (Sandbox Code Playgroud)

第二个实现didSelectAnnotationViewMapView 的委托方法来处理引脚选择上的某些内容didSelectAnnotationView。您可以从.

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
{
    //Pin clicked, do your stuff here
}
Run Code Online (Sandbox Code Playgroud)


Rob*_*Rob 5

首先,值得注意的是,标准用户体验是提供一个标注气泡,向用户显示足够的信息以确认这是预期的注释视图,然后让标注包括左侧和/或右侧附件视图(例如左侧的按钮和/或或者在标注的右侧),然后让用户使用该注释执行一些额外的任务.请参阅位置和地图编程指南中的创建标注.

金门大桥

但是,如果您想在用户点击注释时立即执行其他操作,请设置注释视图,以便(a)它没有标注; 然后(b)实现MKMapViewDelegate方法,didSelectAnnotationView以便在用户点击注释视图时执行您想要的任何任务.


例如,假设您已delegate为地图视图指定了,则可以在Swift 3/4中执行以下操作:

private let reuseIdentifier = "MyIdentifier"

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? MKPinAnnotationView
    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        annotationView?.tintColor = .green                // do whatever customization you want
        annotationView?.canShowCallout = false            // but turn off callout
    } else {
        annotationView?.annotation = annotation
    }

    return annotationView
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    // do something
}
Run Code Online (Sandbox Code Playgroud)

或者在Swift 2中:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView
    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        annotationView?.tintColor = UIColor.greenColor()  // do whatever customization you want
        annotationView?.canShowCallout = false            // but turn off callout
    } else {
        annotationView?.annotation = annotation
    }

    return annotationView
}

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    // do something
}
Run Code Online (Sandbox Code Playgroud)


小智 5

雨燕5

override func viewDidLoad() {
   super.viewDidLoad()
   mapView.delegate = self
}

extension MapViewController: MKMapViewDelegate {

   func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
      print("calloutAccessoryControlTapped")
   }

   func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
      print("didSelectAnnotationTapped")
   }
}
Run Code Online (Sandbox Code Playgroud)