2016-11-08 14 views
0

私はmapkitプロジェクト(swift 3)でカスタム注釈を使用して、マップ上に複数の注釈を表示しています。それは表示され、私はannotationnをクリックすることができますが、初めてです。注釈をもう一度開くには、地図上のどこでもクリックして注釈をもう一度クリックする必要があります。誰も私を助けることができますか?前もって感謝します。MKMapViewはdidSelectコールバックを1回だけ呼び出します

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)が2回あるのですか? 2番目のビューではビューを隠しているので、おそらく1回だけ行うことができます。 – Echizzle

+0

@ Echizzle:そうです。コンパイルすべきではありません。 – shallowThought

+0

あまりにも、私は彼がコンパイルする前にちょうどそれで遊んでいたかもしれないと思った。 – Echizzle

答えて

0

私は解決策を見つけた:ここ

は、私が使用している機能です!クリックされた注釈が選択されているため、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) 
      } 
     } 
} 
関連する問題