MKMapView只调用一次didSelect回调函数。

3
我正在我的MapKit项目(使用Swift 3)中使用自定义注释来在地图上显示多个注释。它能够显示并且我可以点击注释,但只有第一次有效。要再次打开注释,我需要在地图上任意位置点击一下,然后再次点击注释。有人能帮助我吗?提前感谢您。
以下是我使用的函数:
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation
    {
        return nil
    }
    var annotationView = self.map.dequeueReusableAnnotationView(withIdentifier: "Pin")
    if annotationView == nil{
        annotationView = AnnotationView(annotation: annotation, reuseIdentifier: "Pin")
        annotationView?.canShowCallout = false
    }else{
        annotationView?.annotation = annotation
    }
    if (indexPin > 0) {
        indexPin = indexPin - 1
        let pin : PinAnnotation = pinAnotationList[indexPin]
        annotationView?.image = UIImage(named: pin.imageName)
    }
    return annotationView
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)
{
    if view.annotation is MKUserLocation
    {
        return
    }
    let pin = view.annotation as! PinAnnotation
    if pin.userType == "O" {
        if (currentLatitude == 0 || currentLatitude2 == 0) {
             self.showAlert(self, message: "It's necessary to set origin and destiny addresses")
            return
        }
        AppVars.DriverId = pin.userId
        AppVars.VehicleId = pin.vehicleId
        AppVars.LatitudeDriver = pin.coordinate.latitude
        AppVars.LongitudeDriver = pin.coordinate.longitude
        performSegue(withIdentifier: "callDriverPopupSegue", sender: self)
    }
    else {
        let customView = (Bundle.main.loadNibNamed("AnnotationView", owner: self, options: nil))?[0] as! CustomCalloutView
        var calloutViewFrame = customView.frame;
        let point = CGPoint(x: calloutViewFrame.size.width/2 + 15,y :calloutViewFrame.size.height - 10)
        calloutViewFrame.origin = point
        customView.frame = calloutViewFrame;
        customView.titleLabel.text = pin.title
        view.addSubview(customView)
    }
}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    if (view.isKind(of: PinAnnotation.self))
    {
        for subview in view.subviews
        {
            subview.removeFromSuperview()
        }
    }

    if (view.isKind(of: AnnotationView.self))
    {
        for subview in view.subviews
        {
            subview.removeFromSuperview()
        }
    }

}

类 PinAnnotation

import MapKit

class PinAnnotation: NSObject, MKAnnotation {

    var coordinate: CLLocationCoordinate2D
    var userId: Int!
    var vehicleId:Int!
    var userType: String!
    var imageName: String!
    var title: String!
    init(coordinate: CLLocationCoordinate2D) {
        self.coordinate = coordinate
    }
}

类 AnnotationView

import MapKit

class AnnotationView: MKAnnotationView
{
} 

为什么你有两个委托函数(didSelectAnnotationView)?在第二个函数中,你隐藏了视图,这可能是为什么你只能执行一次的原因。 - Echizzle
@ Echizzle: 对的。它甚至不应该编译通过。 - shallowThought
是的,我也这么认为。我觉得他可能只是在编译之前随便玩玩而已。 - Echizzle
抱歉...我不是Swift的专家。但这三个函数不一样,对吗?! - Almeida
1个回答

8

我找到了一个解决方案!这种情况发生在调用performSegue(withIdentifier: "callDriverPopupSegue", sender: self)时,在didSelect中保持选定的注释。因此,我在mapview控制器中添加了以下代码来取消选择注释,之后我就能够第二次点击了。

override func viewWillAppear(_ animated: Bool) {
        DispatchQueue.main.async {
            for item in self.map.selectedAnnotations {
                self.map.deselectAnnotation(item, animated: false)
            }
        }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接