2015-09-29 2 views
15

Swift 1.2からSwift 2.0に変更しようとしていますが、変更の最後です。現在、私はMapViewControllerを変更していますが、エラーや警告はありませんが、私のピン(annotationView)のカスタムイメージはピンに割り当てられておらず、デフォルトのもの(赤い点)を表示しています。ここでiOSのannotationViewのカスタムピンイメージ

私のコードで私はすべてがうまくだと思うが、それはまだ働いていないので、私はあなたには、いくつかのヒントで私を助けることを願って:事前に

func parseJsonData(data: NSData) -> [Farmacia] { 

    let farmacias = [Farmacia]() 

    do 
    { 
     let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary 

     // Parse JSON data 
     let jsonProductos = jsonResult?["farmacias"] as! [AnyObject] 

     for jsonProducto in jsonProductos { 

      let farmacia = Farmacia() 
      farmacia.id = jsonProducto["id"] as! String 
      farmacia.nombre = jsonProducto["nombre"] as! String 
      farmacia.location = jsonProducto["location"] as! String 

      let geoCoder = CLGeocoder() 
      geoCoder.geocodeAddressString(farmacia.location, completionHandler: { placemarks, error in 

       if error != nil { 
        print(error) 
        return 
       } 

       if placemarks != nil && placemarks!.count > 0 { 

        let placemark = placemarks?[0] 

        // Add Annotation 
        let annotation = MKPointAnnotation() 
        annotation.title = farmacia.nombre 
        annotation.subtitle = farmacia.id 
        annotation.coordinate = placemark!.location!.coordinate 

        self.mapView.addAnnotation(annotation) 
       } 

      }) 
     } 
    } 
    catch let parseError { 
     print(parseError) 
    } 

    return farmacias 
} 

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 

    let identifier = "MyPin" 

    if annotation.isKindOfClass(MKUserLocation) { 
     return nil 
    } 

    // Reuse the annotation if possible 
    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 

    if annotationView == nil 
    { 
     annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
     annotationView!.canShowCallout = true 
    } 

    annotationView!.image = UIImage(named: "custom_pin.png") 

    let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure) 
    annotationView!.rightCalloutAccessoryView = detailButton 

    print(annotationView!.image) 

    return annotationView 
} 

おかげで、

よろしく。ここで

答えて

15

答えている:それはelseブロックで初期化されていないannotationViewを持っているよう

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 

    let identifier = "MyPin" 

    if annotation.isKindOfClass(MKUserLocation) { 
     return nil 
    } 

    let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure) 

    if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) { 
     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin") 
     annotationView.canShowCallout = true 
     annotationView.image = UIImage(named: "custom_pin.png") 
     annotationView.rightCalloutAccessoryView = detailButton 
    } 
    else { 
     annotationView.annotation = annotation 
    } 

    return annotationView 
} 

よろしく

+1

解決策をお寄せいただきありがとうございます。それも私を助けた –

+0

はスウィフト2.2のために動作しません – DeyaEldeen

+0

ダウン投票しました。 1) "annotationView"インスタンスはif-letスコープに作成され、elseスコープには存在しません。 2)アノテーションビューをデキューすることができれば、そのインスタンスを使用し、新しいインスタンスを作成する必要はありません。新しいインスタンスは、再利用可能なキューからデキューできないelseスコープ内に作成する必要があります。 – esbenr

41

は受け入れ答えは、動作しません。

これは優れた解決方法です。それが可能な場合注釈ビューをデキューするか、新しいものではない場合に作成:

スウィフト3、4

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    // Don't want to show a custom image if the annotation is the user's location. 
    guard !(annotation is MKUserLocation) else { 
     return nil 
    } 

    // Better to make this class property 
    let annotationIdentifier = "AnnotationIdentifier" 

    var annotationView: MKAnnotationView? 
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) { 
     annotationView = dequeuedAnnotationView 
     annotationView?.annotation = annotation 
    } 
    else { 
     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 
     annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) 
    } 

    if let annotationView = annotationView { 
     // Configure your annotation view here 
     annotationView.canShowCallout = true 
     annotationView.image = UIImage(named: "yourImage") 
    } 

    return annotationView 
} 

スイフト2.2

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    // Don't want to show a custom image if the annotation is the user's location. 
    guard !annotation.isKindOfClass(MKUserLocation) else { 
     return nil 
    } 

    let annotationIdentifier = "AnnotationIdentifier" 

    var annotationView: MKAnnotationView? 
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) { 
     annotationView = dequeuedAnnotationView 
     annotationView?.annotation = annotation 
    } 
    else { 
     let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 
     av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) 
     annotationView = av 
    } 

    if let annotationView = annotationView { 
     // Configure your annotation view here 
     annotationView.canShowCallout = true 
     annotationView.image = UIImage(named: "yourImage") 
    } 

    return annotationView 
} 
+1

これは、受け入れられた答えよりもはるかに多くを助けました、ありがとう@アントレイ・ゴードデフ – Alk

+1

うわー!!!これは真剣に私を救った!私はいくつかの深刻なperf問題を抱えていましたが、本当に助けられた識別子を適切に使用する方法についてこの例を見ています。私はそれを正しく使い始めると思った。これは受け入れられた答えでなければなりません! – BryHaw

+0

素晴らしい!ありがとうございました。 – Raja

0

SWIFT 3,4

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

    if annotation.isKind(of: MKUserLocation.self) { 
     return nil 
    } 

    let annotationIdentifier = "AnnotationIdentifier" 

    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) 
    if (pinView == nil) { 
     pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 
    } 

    pinView?.canShowCallout = false 

    return pinView 
}