I am trying to make a custom "user location" pin, with heading rotation based on users location heading.
I used this answer: https://stackoverflow.com/a/58363556/894671 as a base and managed to get up and running a custom pin, that rotates based on the heading.
The problem:
While testing on a device, it seems, that the transformation using the provided heading is not correct. Only at 0/360 degrees it shows up correctly, but If I rotate around, I am seeing default MKMapKit shown heading to be correctly rotating, while my custom icon manages to rotate twice in that same time.
Please see the attached video: https://i.imgur.com/3PEm2MS.mp4
Demo uploaded here: https://github.com/GuntisTreulands/Demo123
But for all intents and purposes, here is AnnotationView:
class AnnotationView : MKAnnotationView, HeadingDelegate {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
func headingChanged(_ heading: CLLocationDirection) {
// For simplicity the affine transform is done on the view itself
UIView.animate(withDuration: 0.1, animations: { [unowned self] in
self.transform = CGAffineTransform(rotationAngle: CGFloat(heading * .pi / 180.0 ))
})
}
}
and heading is forwarded from here:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
if let lastLocation = locations.last {
userLocationAnnotation.coordinate = lastLocation.coordinate
}
}
I can't figure out, why my location heading is acting so weird.