Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GPS coordinates in degrees to calculate distances

Tags:

iphone

map

gps

ios4

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?

like image 938
Durga Avatar asked Aug 09 '11 09:08

Durga


3 Answers

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.

like image 173
Parth Adroja Avatar answered Oct 31 '22 15:10

Parth Adroja


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.

like image 24
Toastor Avatar answered Oct 31 '22 14:10

Toastor


  • (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.

like image 33
RabinDev Avatar answered Oct 31 '22 15:10

RabinDev