2016-11-08 97 views
0

我在我的mapkit項目中使用自定義註釋(在swift 3中)在地圖上顯示多個註釋。這是顯示,我可以點擊註釋,但只有第一次。爲了再次打開註解,我需要點擊地圖上的任何地方,然後再次單擊註釋。有人能幫助我嗎?先謝謝你。MKMapView只調用didSelect回調一次

下面是我使用的功能:

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 
{ 
} 
+0

爲什麼你有委託函數(didSelectAnnotationView)兩次?第二個是你隱藏的意見,這可能是你爲什麼只能做一次。 – Echizzle

+0

@ Echizzle:對。它甚至不應該編譯。 – shallowThought

+0

是的,我想也許他只是在編譯前就玩過它。 – Echizzle

回答

0

我已經找到了解決方案!當在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) 
      } 
     } 
}