2016-09-30 3 views
2

Hier ist ein kleines Problem ..MKAnnotationView nicht angezeigt Bild

Ich habe einige Code getan, die das Bild des Stiftes ändern sollte, dass die in mapView fallen gelassen zu bekommen. Die URL von "taxi.png" funktioniert in einer Bildansicht, das ist also nicht das aktuelle Problem.

ich auch versucht:

pinView?.image = UIImage(named: "taxi.png") 

Code:

if pinView == nil{ 
     pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     pinView?.canShowCallout = true 
     var imageTaxi = UIImage(named: "taxi.png")! 
     imageTaxi = imageTaxi.withAlignmentRectInsets(UIEdgeInsetsMake(0, 0, imageTaxi.size.height/2, 0)) 
     pinView?.image = imageTaxi 
     } 

     let button = UIButton(type: .detailDisclosure) 
     pinView?.rightCalloutAccessoryView = button 
     return pinView 

niemand sehen, was hier festgelegt werden soll, um das Bild zu sehen?

EDIT: Ich habe jetzt das und ich habe immer noch die Standard-rot Pinpoint ..

let annotation = CustomPin() 
annotation.coordinate = location 
annotation.title = "Boek Taxi" 
annotation.subtitle = "" 
let image = UIImage(named: "taxi.png") 

annotation.Image = image 
self.mapView.addAnnotation(annotation) 
let reuseId = "pin" 
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) 

    if pinView == nil{ 
     pinView = MKPinAnnotationView(annotation: CustomPin() as MKAnnotation, reuseIdentifier: reuseId) 
     pinView?.canShowCallout = true 

    } else { 
     pinView?.annotation = CustomPin() as MKAnnotation 
    } 

    let cpa = annotation as! CustomPin 

    pinView?.image = cpa.Image 

    let button = UIButton(type: .detailDisclosure) 
    pinView?.rightCalloutAccessoryView = button 
    return pinView 
    } 

Irgendwelche Dinge, die ich übersehen?

+0

Haben Sie einen Blick auf diese [Custom Pin Bild] (http://stackoverflow.com/a/38159048/4080925) – MwcsMac

+0

Bestätigen Sie, dass dieser Code überhaupt genannt zu werden (Set Breakpoint und/oder eine Log-Anweisung drucken). Vielleicht wurde der Delegat nicht angegeben oder die Methodensignatur stimmt nicht. Wenn das nicht der Fall ist, klären Sie bitte: Wenn Sie sagen, dass es ein "Problem" gibt, was genau sehen Sie? Roter Stift? Nichts? Absturz? – Rob

Antwort

-1

Versuchen Sie, diese

eine Klasse für die benutzerdefinierte Anmerkung Beispiel erstellen:

class CustomPin: MKPointAnnotation { 

    let Image = UIImage 

} 

Dann in Ihrer Funktion für die Anmerkungsansicht fügen diese

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
     let reuseId = "image" 

     var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) 
     if pinView == nil { 

      pinView = MKAnnotationView(annotation: customPin as MKAnnotation, reuseIdentifier: reuseId) 
      pinView!.canShowCallout = true 

     } else { 

      pinView!.annotation = customPin as MKAnnotation 
     } 

     let cpa = annotation as! CustomPin 
     pinView!.image = cpa.Image 

     let button = UIButton(type: UIButtonType.detailDisclosure) 

     pinView!.rightCalloutAccessoryView = button 


     return anView 


     } 

schließlich das benutzerdefinierte Bild, wenn Sie erstellen die Annotation selbst in Ihrem viewDidLoad() oder was auch immer Sie wollen wie folgt:

       let Annotation = customPin() 
           Annotation.coordinate = //cooridnate 
           Annotation.title = //title 
           Annotation.subtitle = //subtitle 

           let image = UIImage(named: "taxi.png") 
           Annotation.Image = image 

hoffe, das hilft

+0

@Rob funktioniert gut in meiner App, die genau das tut, was er fragt –

+0

Danke für Ihre Antwort @TripPhillips, aber wenn ich es so mache, habe ich immer noch den roten Standard Pinpoint .. Könnten Sie bitte den Code, den ich in meinem hochgeladen Frage Beitrag. Ich wäre wirklich dankbar – royvano

+0

@royvano Sie müssen MKAnnotationView anstelle von MKAnnotationPinView verwenden. Das ist dein Problem. Wo Sie pinView definieren, verwenden Sie MKAnnotationView. Wenn Sie MKAnnotationPinView verwenden, erhalten Sie jedes Mal eine PIN. –