In MK this was strait forward, but I can't seem to find anything that works with the new MapKit.
Requirement: When location changes, animate smoothly the annotation from oldValue to newValue.
So I have tried almost everything, but eventually landed on the notion of fractionalizing the delta between oldValue and newValue by 100, then brute force updating the position for each fraction. Here is the function I am working with:
func animateAnnotationMove(from oldCoordinate: CLLocationCoordinate2D, to newCoordinate: CLLocationCoordinate2D, steps: Int = 100, interval: TimeInterval = 0.05) {
let latitudeDiff = (newCoordinate.latitude - oldCoordinate.latitude) / Double(steps)
let longitudeDiff = (newCoordinate.longitude - oldCoordinate.longitude) / Double(steps)
var currentStep = 0
Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { [weak self] timer in
guard currentStep < steps else {
timer.invalidate() // Stop the timer
return
}
let nextLatitude = oldCoordinate.latitude + (latitudeDiff * Double(currentStep))
let nextLongitude = oldCoordinate.longitude + (longitudeDiff * Double(currentStep))
let nextCoordinate = CLLocaionCoordinate2D(latitude: nextLatitude, longitude: nextLongitude)
DispatchQueue.main.async {
self?.updateGuestAnnotationPosition(with: nextCoordinate)
}
currentStep += 1
}
}
My concern is that this is not very efficient as I scale, and with SwiftUI's ability to just animate in many other areas, maybe I'm missing something obvious? My location data updates every 5 seconds, so the thinking here is to break that down into 100 function calls (so every 0.05 seconds) and animate the delta.
This is how I got my custom annotation to animate including the Map keeping the annotation in the center.