Q: How to rotate marker in swift by heading? 
Hi Guys i'm trying to update the location of this annotation by receiving heading from socket so every time i update the heading the image return to its original state then goes back to the new heading received from socket also after the update the image appears like it's rotated left from the heading received from socket(little deviation) here's the code i'm using to update the heading:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {


        guard !(annotation is MKUserLocation) else {
            return nil
        }
        let annotationIdentifier = "marker"
        var annotationView: MKAnnotationView?
         let unitAnnotation = annotation as? Unit
        if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
            annotationView = dequeuedAnnotationView
            annotationView?.annotation = annotation
        }else {
            annotationView = unitAnnotation?.annotationView()

            annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
        }


        if let annotationView = annotationView {

            annotationView.canShowCallout = true
            let image = #imageLiteral(resourceName: "Arrow32")
                let imageHeight  = image.size.height
            annotationView.image = image
            annotationView.tintColor = UIColor.blue

         annotationView.centerOffset = CGPoint(x: 0, y: -imageHeight / 2)
            self.addBounceAnimationToView(view: annotationView)
        }

        UIView.animate(withDuration: 0.5) {
            if let heading = unitAnnotation?.heading{
                //annotationView?.annotation?.= unitAnnotation?.coordinate

            annotationView?.transform = self.mapKit.transform.rotated(by: CGFloat(self.degreesToRadians(degrees: heading+20)))
            }
        }
        return annotationView

    } 

Any Help will be much appreciated thanks alot.

A: locationManager.startUpdatingHeading()  



func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
            UIView.animate(withDuration: 0.5) {
                let angle = CGFloat(newHeading.trueHeading).toRadians
                self.markerView.transform = CGAffineTransform(rotationAngle: angle)
            }
        }

An extension to convert to radians
extension CGFloat {
        var toRadians: CGFloat { return self * .pi / 180 }
        var toDegrees: CGFloat { return self * 180 / .pi }
    }


A: You need to save the old heading add it to the new one then make the transform
