GPS coordinates in degrees to calculate distances

Viewed 6262

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?

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.

Related