11

On the iPhone, I get the user's location in decimal degrees, for example: latitude 39.470920 and longitude = -0.373192; That's point A.

I need to create a line with another GPS coordinate, also in decimal degrees, point B. Then, calculate the distance (perpendicular) between the line from A to B and another point C.

The problem is I get confused with the values in degrees. I would like to have the result in meters. What's the conversion needed? How will the final formula to compute this look like?

Toastor
  • 8,980
  • 4
  • 50
  • 82
Durga
  • 933
  • 1
  • 9
  • 12
  • I also have the same problem, and neither one of the 2 answers here currently solve that. RabinDev's answer got closer, but he didn't solve the degrees to meters conversion problem. Any tips? – Rodrigo Castro Dec 19 '11 at 04:35

3 Answers3

29

Why don't you use CLLocations distanceFromLocation: method? It will tell you the precise distance between the receiver and another CLLocation.

CLLocation *locationA = [[CLLocation alloc] initWithLatitude:12.123456 longitude:12.123456];
CLLocation *locationB = [[CLLocation alloc] initWithLatitude:21.654321 longitude:21.654321];

CLLocationDistance distanceInMeters = [locationA distanceFromLocation:locationB];

// CLLocation is aka double

[locationA release];
[locationB release];

It's as easy as that.

Toastor
  • 8,980
  • 4
  • 50
  • 82
  • hi Toastor Distance calculation any sample example send it please – Durga Aug 09 '11 at 09:54
  • Done. Things like that are easy to learn from the reference library though. I posted the link in my answer, you may want to take a look at it. – Toastor Aug 09 '11 at 10:06
1

Swift 3.0+

Only calculate distance between two coordinates:

let distance = source.distance(from: destination)

When you have array of locations:

To get distance from array of points use below reduce method.

Here locations is array of type CLLocation.

 let calculatedDistance = locations.reduce((0, locations[0])) { ($0.0 + $0.1.distance(from: $1), $1)}.0

Here you will get distance in meters.

Parth Adroja
  • 13,198
  • 5
  • 37
  • 71
1
  • (CLLocationDistance)distanceFromLocation:(const CLLocation *)location is the method to get the distance from on CLLocation to another.

Your problem is also of finding the shortest line between a line (A,B) and point C. I guess if your 3 CLLocations are near ( less than a few kilometers apart), you can do the math "as if" the coordinates are points on a single plane, and use this in C++, or this or this and just use the CLLocations "as if" they were x and y coordinates on a plane.

If your coordinates are far away, or exact accuracy is important then the spherical shape of the earth matters, and you need to do things using great circle distance and other geometry on the face of a sphere.

Community
  • 1
  • 1
RabinDev
  • 658
  • 3
  • 13