Custom annotation image rotates only at the beginning of the Program (Swift- iOS)

Please help out a beginner iOS developer here! 🙂 So, I have a timer which gets the latitude and longitude of a bus periodically from a xml sheet which provides real-time locations of the bus. I was able to setup the parser, animate the bus movement and setup a custom (arrow) image for the bus.

However, the problem is, I’m not able to able to rotate the bus-annotation everytime I get new values for the latitude and longitude. The busAnnotation rotates only when the program starts, but not everytime the direction of the bus is updated.

  • How can I check if my AVPlayer is buffering?
  • How to create some kind of Splash screen/Launching screen, which disappears after App loaded? (React Native)
  • Searching/Filtering a custom class array with a NSPredicate
  • Remove Next / Previous buttons (inputAccessoryView) for Custom Keyboard in iOS8 WebView
  • Xcode 7 Null passed to a callee that requires a non-null argument
  • How can I speed up a UITableView?
  • Below is my xml parser function, which calculates the busDirection everytime the location changes. The value of busDirection changes correctly with location changes, but it doesn’t apply to the rotation for some reason as the timer goes on. The movement of the annotation is also well-animated, it just doesn’t rotate other than when the program is started.

    // Some constants
    var oldLatitude: Double? = 44.97234
    var oldLongitude: Double? = -93.2446329
    var busDirection: CLLocationDirection()
    func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: NSDictionary!) {
        if (elementName == "vehicle") {
            var latitude = attributeDict["lat"]?.doubleValue            // Get latitude of bus from xml
            var longitude = attributeDict["lon"]?.doubleValue           // Get longitude of bus from xml
            if (oldLatitude == latitude && oldLongitude == longitude) {
            else {  // Update bus location         
                busDirection = directionBetweenPoints(MKMapPointForCoordinate(CLLocationCoordinate2DMake(oldLatitude!, oldLongitude!)), destinationPoint: MKMapPointForCoordinate(CLLocationCoordinate2DMake(latitude!, longitude!)))       // Get direction of bus:
                UIView.animateWithDuration(0.5) {
                    self.busAnnotation.coordinate = CLLocationCoordinate2DMake(latitude!, longitude!)                  
                    self.mapView(, viewForAnnotation: self.busAnnotation).annotation = self.busAnnotation
                oldLatitude = latitude
                oldLongitude = longitude

    Here is the ‘viewForAnnotation’ that I implemented in order to change the image of the annotation and supposedly rotate it

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        let reuseId = "pin" 
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView  
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.image = imageWithImage(UIImage(named:"arrow.png")!, scaledToSize: CGSize(width: 25.0, height: 25.0))       // Set custom image for annotation and resize it         
        else {
            pinView!.annotation = annotation
        // Rotate the annotation.
        pinView?.transform = CGAffineTransformRotate(, CGFloat(degreesToRadians(self.busDirection)))
        return pinView

    These are some of the helper functions that I used in the above methods:

    // For rotating bus annotations:
    func degreesToRadians(degrees: Double) -> Double { return degrees * M_PI / 180.0 }
    func radiansToDegrees(radians: Double) -> Double { return radians * 180.0 / M_PI }
    func directionBetweenPoints(sourcePoint: MKMapPoint, destinationPoint : MKMapPoint) -> CLLocationDirection {
        let x : Double = destinationPoint.x - sourcePoint.x;
        let y : Double = destinationPoint.y - sourcePoint.y;
        return fmod(radiansToDegrees(atan2(y, x)), 360.0) + 90.0;

    Do you guys think I’m missing something here? How would I rotate the bus-annotation everytime the locations are updated? Thanks in advance.

    Solutions Collect From Internet About “Custom annotation image rotates only at the beginning of the Program (Swift- iOS)”

    The rotation only occurs the first time the map view creates the view for the annotation. Rather than create a new view every time an annotation updates, it simply moves the view. That’s much more efficient for the map view to do.
    So, what you should do is make the pinView a property, instantiate it in your init method, return that in mapView(, viewForAnnotation:) and apply the rotation directly to that pinView in your animation block.

    UIView.animateWithDuration(0.5) {
        self.busAnnotation.coordinate = CLLocationCoordinate2DMake(latitude!, longitude!)                 
        if let pv = self.pinView {
            pv.transform = CGAffineTransformRotate(, CGFloat(degreesToRadians(self.busDirection)))