Custom MKAnnotation does not move when dialing coordinates

I have a custom MKAnotation setup

class Location: NSObject, MKAnnotation { var id: Int var title: String? var name: String var coordinate: CLLocationCoordinate2D { didSet{ didChangeValueForKey("coordinate") } } 

when I set the coordinate, it does not move the output on the map. I added “didSet” with the keyword coordinate, as I found that people say that it will force it to update. but nothing, he does not move. I checked that the lat / long has changed and is correct, the pin just does not move. The header updates and displays the change.

I have to accept it because it is a user annotation. any help?

+6
source share
1 answer

If you intend to manually post these change events, you must call willChangeValue(forKey:) in willSet . In Swift 4:

 var coordinate: CLLocationCoordinate2D { willSet { willChangeValue(for: \.coordinate) } didSet { didChangeValue(for: \.coordinate) } } 

Or in Swift 3:

 var coordinate: CLLocationCoordinate2D { willSet { willChangeValue(forKey: #keyPath(coordinate)) } didSet { didChangeValue(forKey: #keyPath(coordinate)) } } 

Or, which is much simpler, just specify it as a dynamic property and this message will be made for you.

 dynamic var coordinate: CLLocationCoordinate2D 

For Swift 2, see the previous version of this answer .

+11
source

All Articles