3

I try to create a custom "badget" for my MKPointAnnotation in swift, but it fails as MKPointAnnotation does not have any property like image

 var information = MKPointAnnotation()
    information.coordinate = location 
    information.title = "Test Title!"
    information.subtitle = "Subtitle"
    information.image = UIImage(named: "dot.png") //this is the line whats wrong
    Map.addAnnotation(information)

Anyone figured out a swift like solution for that?

akashivskyy
  • 44,342
  • 16
  • 106
  • 116
Fabian Boulegue
  • 6,476
  • 14
  • 47
  • 72
  • 1
    Dig into `MapKit` much more, `MKPointAnnotation` isn't responsible for the display of the annotation. That functionality is handled by `MKAnnotationView` and the `MKMapViewDelegate` – David Berry Sep 02 '14 at 15:23
  • 1
    See http://stackoverflow.com/questions/24467408/swift-add-mkannotationview-to-mkmapview, http://stackoverflow.com/questions/24523702/stuck-on-using-mkpinannotationview-within-swift-and-mapkit –  Sep 02 '14 at 15:42

1 Answers1

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

    if !(annotation is MKPointAnnotation) {
        return nil
    }

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("demo")
    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "demo")
        annotationView!.canShowCallout = true
    }
    else {
        annotationView!.annotation = annotation
    }

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

    return annotationView

}
Andres Marin
  • 251
  • 3
  • 12