如何在放置 pin 时打开 MKPinAnnotationView

How to have a MKPinAnnotationView open when pin is dropped

我目前正在使用此功能丢弃可重复使用的图钉:

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
}

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

我试过这些方法:

pinView?.isSelected = true

mapView.selectedAnnotations(reuse, animated: true)

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

第二种方法错误为 "Cannot call value of non-function type '[MKAnnotation]'"

您需要 MKMapView 中的此方法,使用:

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

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

mapView.selectAnnotation(annotation, animated: true)

只需在返回 pinView 之前调用它就可以了。

希望对您有所帮助。