royvano royvano - 2 months ago 14
Swift Question

MKAnnotationView doesn't display image

Here's a small problem..

I've done some code which should change the image of the pin that get's dropped in mapView. The url of "taxi.png" works on an image view so that's not the current problem.

I also tried:

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


Does anybody see what should be fixed here to view the image?

EDIT:
I now have this and I still have the standard red 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
}


Any things I've over looked?

Answer

Try this

create a class for your custom annotation Example :

class CustomPin: MKPointAnnotation {

   let Image = UIImage

 }

Then in your function for the annotation view do this

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


        }

Finally add the custom image when you create the annotation itself in your viewDidLoad() or whatever you want like this:

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

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

Hope this helps