2016-08-02 27 views
3

Ich habe andere Stack-Posts und -Tutorials verfolgt, um mein Annotationsbild in ein benutzerdefiniertes Bild zu ändern, aber es scheint nicht zu funktionieren. Es werden keine Fehler oder Laufzeitfehler angezeigt. Das Anmerkungsabbild ändert sich nicht.Swift - Ändern des Annotationsbilds funktioniert nicht

Nur nebenbei setze ich einen Breakpoint auf der Linie annotationView!.image = UIImage(named: "RaceCarMan2png.png") und es zeigt, dass die Linie aufgerufen wird, aber noch nichts passiert. Ich würde deine Hilfe sehr schätzen. Vielen Dank.

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    if annotation is MKUserLocation { 
     return nil 
    } 

    let identifier = "MyCustomAnnotation" 

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 
    if annotationView == nil { 
     annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
     annotationView?.canShowCallout = true 

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

    } else { 
     annotationView!.annotation = annotation 
    } 


    configureDetailView(annotationView!) 

    return annotationView 
} 

func configureDetailView(annotationView: MKAnnotationView) { 
     annotationView.detailCalloutAccessoryView = UIImageView(image: UIImage(named: "url.jpg")) 
} 

Antwort

4

Das Problem ist, dass Sie‘ Verwenden Sie MKPinAnnotationView. Wenn Sie MKAnnotationView verwenden, werden Sie Ihr Bild sehen:

class ViewController: UIViewController, MKMapViewDelegate { 

    @IBOutlet weak var mapView: MKMapView! 

    private func addAnnotation(coordinate coordinate: CLLocationCoordinate2D, title: String, subtitle: String) { 
     let annotation = MKPointAnnotation() 
     annotation.coordinate = coordinate 
     annotation.title = title 
     annotation.subtitle = subtitle 
     mapView.addAnnotation(annotation) 
    } 

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
     if annotation is MKUserLocation { return nil } 

     let identifier = "CustomAnnotation" 

     var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 

     if annotationView == nil { 
      annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
      annotationView!.canShowCallout = true 
      annotationView!.image = UIImage(named: "star.png")! // go ahead and use forced unwrapping and you'll be notified if it can't be found; alternatively, use `guard` statement to accomplish the same thing and show a custom error message 
     } else { 
      annotationView!.annotation = annotation 
     } 

     return annotationView 
    } 

    ... 
} 

Nachgeben:

Chicago star

In der Vergangenheit (zB iOS 8), die image von MKPinAnnotationView Einstellung schien gut zu funktionieren, aber in iOS 9.x, scheint es trotzdem einen Pin zu verwenden. Das ist nicht völlig unvernünftig für eine Klasse namens MKPinAnnotationView, und die Verwendung MKAnnotationView vermeidet dieses Problem.

+0

Vielen Dank, dass funktioniert! –

0

Anruf, dass vor der Rückkehr

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

return annotationView 

und mit Breakpoint überprüfen, ob es in Ordnung ist, wenn innerhalb zeigen uns Screenshot von Ihrem Bild in Projekt auch :)