Ezequiel Santos
iOS Developer & Creative Technologist

Visualizing Real-Time Map Rotation with GPS Coordinates on iOS

Smooth vehicle animations in navigation apps like Uber or Google Maps seem effortless—but under the hood, you're only given discrete GPS coordinates. This post explores how to derive orientation and simulate real-time movement using basic geometry and Swift. The approach is platform-agnostic but demonstrated here using CLLocationCoordinate2D, trigonometric functions like atan, and foundational vector math.

Uber map screenshot
Figure 1. Car smoothly rotating along streets in a navigation app.

We start by imagining a Cartesian plane. Each time the vehicle moves, it emits a new GPS coordinate: latitude and longitude. These correspond to X and Y.

Cartesian plane
Figure 2. Representation of movement as points on a 2D plane.

Now, let’s look at two sequential points:

Points A and B on map
Figure 3. Points A and B captured from the vehicle's movement.

Calculating Direction from Two Coordinates

To compute the heading angle (tilt), use the slope between A and B.

With these values:


m = tan(α) = ΔY / ΔX

Then, using the arctangent:


α = atan(m)
Arctangent domain
Figure 4. Arctangent domain is limited to [−π/2, π/2]; quadrant corrections may be needed.

In Swift, you work with CLLocationCoordinate2D for geographic points. To calculate the angle between two, you do something like this:

This gives the rotation angle (heading) in radians, usable in `CGAffineTransform` or `MKAnnotationView` updates.

Custom vehicle annotation on map
Figure 5. Example of applying this calculation to animate a car icon on a map.

Conclusion

By converting coordinate deltas into an angle via atan, you can animate vehicles naturally along routes. The principles remain consistent across platforms and work elegantly in Swift with minor math and trigonometry.

Further Reading