Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distance between two coordinates with CoreLocation

I need to calculate the distance (in meters and miles) between two coordinates given

How can I do that?

like image 516
isiaatz Avatar asked Aug 28 '09 18:08

isiaatz


People also ask

Can you calculate distance from coordinates?

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.

How do I find the distance between two lat long Swift?

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];

How do you find the distance between two Geohashes?

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.


2 Answers

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

like image 121
Terry Wilcox Avatar answered Sep 28 '22 16:09

Terry Wilcox


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.

like image 27
Mark Chackerian Avatar answered Sep 28 '22 16:09

Mark Chackerian