Search code examples
swiftcore-locationcoordinate

Swift: Coordinates not accurate when using latitude/longitude


I'm using the CLLocationManager in Swift. When I display location.coordinate, it provides me with the result:

37.787358900000001, longitude: -122.408227

However, if I display. location.coordinate.latitude, the result is

37.7873589

Do you know how I can get all the decimals?

Thanks, Thomas


Solution

  • You are seeing 37.787358900000001 instead of 37.7873589 because of the nature of representing floating point numbers in a computer.

    That said, it is pointless to worry about decimal places beyond the 6th position because even changing the 9 to an 8 would result in a change of about 1 centimeter. The GPS system is currently accurate to about +/- 5 meters, and with new chips being deployed by Broadcom that can be reduced to about +/- 30 centimeters in the near future.

    By the way, do you know why the altitude keeps updating/changing, even when the device is standing still on a table?

    Altitude calculations are done by computing the distance to the visible GPS satellites which orbit the Earth every 12 hours. Since the satellites are constantly moving, and different satellites come into view, these calculations do tend to fluctuate. This article though a bit dated is enlightening.