2

我在我的 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
    }
}

类注解视图

import MapKit

class AnnotationView: MKAnnotationView
{
} 
4

1 回答 1

6

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

override func viewWillAppear(_ animated: Bool) {
        DispatchQueue.main.async {
            for item in self.map.selectedAnnotations {
                self.map.deselectAnnotation(item, animated: false)
            }
        }
}
于 2016-11-08T21:03:11.247 回答