3

Im setting a custom image for the user location on a map like this:

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

    if annotation.isEqual(mapView.userLocation) {

        let identifier = "User"

        var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

        if annotationView == nil{
            annotationView = CustomPointAnnotation(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = true

        } else {
            annotationView!.annotation = annotation
        }

        annotationView!.image = UIImage(named: userImage)

        return annotationView

    }
    return nil
}

Ive change the image property, how can I tell the map to update the MKAnnotationView?

pbush25
  • 5,228
  • 2
  • 26
  • 35

3 Answers3

1

This solutions works for me:

let userLocation = mapView.view(for: mapView.userLocation)
userLocation?.image = UIImage(named: "newImage")
userLocation?.isHidden = true
userLocation?.isHidden = false
Álvaro
  • 11
  • 2
1

One of my answers - https://stackoverflow.com/a/57868438/7505665

I had similar problem and solution in my case was this code

self.mapView.showsUserLocation = false
self.mapView.showsUserLocation = true
ShadeToD
  • 403
  • 9
  • 22
0

I think you want to remove the annotations and then re-add them and that will force the map to update. Editing an existing annotation directly doesn't force the map to redraw.

nwales
  • 3,521
  • 2
  • 25
  • 47