I need to calculate the distance (in meters and miles) between two coordinates given
How can I do that?
The distance formula is: √[(x₂ - x₁)² + (y₂ - y₁)²]. This works for any two points in 2D space with coordinates (x₁, y₁) for the first point and (x₂, y₂) for the second point.
You can use distanceFromLocation to find the distance between two coordinates. Code Snippets: CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1]; CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2]; CLLocationDistance distance = [loc1 distanceFromLocation:loc2];
The distance between two adjacent geohashes depends on the latitude & longitude of the center points. In order to get an accurate distance of your bounding box you need to get the adjacent boxes, calculate their center lat/lon and get the distance.
Returns the distance (in meters) from the receiver’s coordinate to the coordinate of the specified location.
// Deprecated in iOS 3.2 method
- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location
// Correct method
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
CLLocation
The method in the previous answer has been deprecated in iOS 3.2. The new method is the very similar
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
which also returns a distance in meters. It's accounting for the curvature of the earth.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With