0

I'm trying to update a custom annotation image dynamically but couldn't get it done. I have a custom annotation class that is of type MKPointAnnotation which returns image name. Below is the code I have been working with...

class CustomAnnotation: MKPointAnnotation {
    var imageName: String!
}

And based on the data I get from the server I'm changing the image name of the annotation:

self.annotation.imageName = "xyz.png";

I could see the updated image after I exit the view controller and reopening it again. Is there a way to update it dynamically without switching back and forth?

Srujan Simha
  • 3,637
  • 8
  • 42
  • 59
  • See http://stackoverflow.com/questions/6808792/change-uiimage-from-mkannotation-in-the-mkmapview and http://stackoverflow.com/questions/10439335/change-annotation-image-after-its-created for one approach (will need to convert to Swift). Another approach is to create a custom annotation _view_ class that observes changes to its associated annotation's imageName and updates its (the view's) image property. –  Feb 17 '15 at 20:56
  • @Anna I did exactly the way you suggested but in Swift. It's throwing error at this line of code `var test: MKAnnotationView = self.mapView.viewForAnnotation(self.annotation);` -> `found nil while unwrapping it.` I didn't understand your second suggestion. Could you give some more insight on that pls? – Srujan Simha Feb 18 '15 at 15:26

0 Answers0