R. Osolinski
R. Osolinski

Reputation: 31

Update image for user location on MKMapView

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?

Upvotes: 3

Views: 1627

Answers (3)

ShadeToD
ShadeToD

Reputation: 490

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

Upvotes: 1

Álvaro
Álvaro

Reputation: 11

This solutions works for me:

let userLocation = mapView.view(for: mapView.userLocation)
userLocation?.image = UIImage(named: "newImage")
userLocation?.isHidden = true
userLocation?.isHidden = false

Upvotes: 1

nwales
nwales

Reputation: 3561

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.

Upvotes: 0

Related Questions