【问题标题】:How to have a MKPinAnnotationView open when pin is dropped当 pin 被丢弃时如何打开 MKPinAnnotationView
【发布时间】:2017-02-07 16:47:39
【问题描述】:

我目前正在使用此功能删除可重复使用的引脚:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{

    guard !(annotation is MKUserLocation) else { return nil }
    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
    }
    pinView?.pinTintColor = UIColor.orange
    pinView?.canShowCallout = true

    let smallSquare = CGSize(width: 30, height: 30)
    let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: smallSquare))
    button.setTitle("Test", for: UIControlState())
    button.addTarget(self, action: #selector(triggerConfirmedLocation), for: .touchUpInside)

    pinView?.leftCalloutAccessoryView = button
    pinView?.rightCalloutAccessoryView = button
    return pinView
}

目前用户必须点击图钉才能看到注释;但是,我需要在删除 pin 时打开注释。

我试过这些方法:

pinView?.isSelected = true

和

mapView.selectedAnnotations(reuse, animated: true)

但是第一种方法什么都不做。

第二种方法的错误是 "不能调用非函数类型'[MKAnnotation]'的值"

【问题讨论】:

标签: ios swift mkmapview mkannotationview


【解决方案1】:

您需要 MKMapView 使用的这个方法:

func selectAnnotation(_ annotation: MKAnnotation, animated: Bool)

您可以像这样从委托覆盖传递注释:

mapView.selectAnnotation(annotation, animated: true)

只需在返回 pinView 之前调用它即可。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    相关资源
    最近更新 更多