I am working on a map application in which i have to animate movement of an aeroplane, based on user location
So i implemented a custom class MKPlaneAndArrow
by subclassing MKOverlay
class MKPlaneAndArrow : NSObject, MKOverlay {
//@objc dynamic var coordinate: CLLocationCoordinate2D
//the above method also does not change location
//to change the coordinate after initialization
var coordinate: CLLocationCoordinate2D {
willSet {
willChangeValue(forKey: #keyPath(coordinate))
}
didSet {
didChangeValue(forKey: #keyPath(coordinate))
}
}
var boundingMapRect: MKMapRect
init( center: CLLocationCoordinate2D, boundingMapRect_: MKMapRect) {
self.coordinate = center
self.boundingMapRect = boundingMapRect_
}
}
Now, when location delegate didUpdateLocations
is called, i want to change the coordinate of MKPlaneAndArrow
overlay to the current location coordinate so i wrote the following inside didUpdateLocations
//if not already added annotation
if !HomeVC.flagAlreadyAddedPlaneAnnotation {
let planeAndArrow = MKPlaneAndArrow.init(center: lastUserLocation.coordinate, boundingMapRect_: mapRect)
mapView.add(planeAndArrow)
//Did updateLocations will not add the annotation again
HomeVC.flagAlreadyAddedPlaneAnnotation = true
}
//get last user location
guard let lastUserLocation:CLLocation = locations.last else {
print("no location")
return
}
//change coordinate of `MKPlaneAndArrow` overlay
for overlay in mapView.overlays {
if let myPlane = overlay as? MKPlaneAndArrow {
myPlane.coordinate = lastUserLocation.coordinate
}
}
I have setup breakpoints and they hit the line myPlane.coordinate = ...
But i don't know why on earth the coordinate change is not visible on map
Animating
If the above code changes the coordinate, it will not animate the change. I also want it to animate. Like movement of user location on Google Maps or apple maps
I was successfully able to change the coordinate of MKAnnotation with animation. But with overlay, i have been searching for hours but could not find a solution
I saw the following questions on SO along with many others
Notes:
Can't Use
MkAnnotation
because these stay the same size if you zoom, while overlay sizes are relative to the zoom scaleIf you need my
MKOverlayRenderer
class, just ask in the comments. My renderer is just drawing an image using `context.draw
Thanks in advance