You are not accepting to click the annotation for the second time

0

I'm using an annotation customization in my mapkit project (swift 3) to show more than one annotation (pin) on the map. It is showing and you can click annotationn, but only the first time. So I can click and annotate and open it, only if I click anywhere on the map and then re-click the annotation again. Has anyone been through this kind of help? Thanks 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
{
}
    
asked by anonymous 08.11.2016 / 17:53

1 answer

1

I just found a solution! The situation occured when I called the "performSegue (withIdentifier:" callDriverPopupSegue ", sender: self)" inside didSelect because the annotation that was clicked remained selected. So I added the code below in the mapview Controller to deselect the annotation. With this annotation is released to open again by click.

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