2

I'm using a custom annotation in my mapkit project (in swift 3) to show multiple annotations on the map. It's showing and I can click on annotationn but only the first time. For openning the annotation again I need to click everywhere on the map and click again the annotation. Could anybody help me ? Thank you in advance.

Here are the functions I'm using:

    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()
        }
    }

}

Class 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
    }
}

Class AnnotationView

import MapKit

class AnnotationView: MKAnnotationView
{
} 
4

1 に答える 1

6

私は解決策を見つけました!クリックされたアノテーションが選択されたままになるため、didSelect で performSegue(withIdentifier: "callDriverPopupSegue", sender: self) を呼び出すと、このような状況が発生しました。そのため、mapviewコントローラーに次のコードを追加して注釈の選択を解除すると、2回目のクリックが可能になりました。

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 に答える