Reputation: 113
I wonder how I calculate the distance between the iPhone user position and a given coordinates stored in a plist for example?
Upvotes: 1
Views: 1448
Reputation: 66
This is the general formula to calculate the instance between to points, in kilometers.
acos(sin(lat1rad) * sin(lat2rad) + cos(lat1rad) * cos(lat2rad) * cos(lon2rad - lon1rad))) * 6378.1
6378.1 is the approx. radius of the Earth in kilometers. Make sure you express de values in radians and not in degrees. Just in case, the easiest way to convert degrees to radians is:
radians = (degrees * 0.01745329) // degrees * pi over 180
EDIT: fixed the formula to convert between degrees and radians. Thank you Ozzah!
Upvotes: 1
Reputation: 44633
You can use the CLLocation
method distanceFromLocation:
to calculate between two CLLocation
objects. You can use CLLocationManager
to get user's current location and you can create a CLLocation
instance for the coordinates using initWithLatitude:longitude:
initializer.
distanceFromLocation:
returns the distance in meters.
Upvotes: 6