views:

2616

answers:

7

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?

+2  A: 

Take a look at the geographical distance and great-circle distance articles on wikipedia. They provide the formulas you need.

David Schmitt
+1  A: 

You should investigate Universal Transverse Mercator (UTM) coordinate system and terms easting and northing. The basic idea is to convert GPS coordinates (which are in WGS84 coordinate system) to distance from some point - in case of UTM northing is a distance from the equator and easting is distance from one selected meridian. Using this conversion you'll be able to get quite good approximation of real distance between two points.

maciejkow
+1. If the original poster wants the end result in metre precision then they had better use coordinates from a more accurate projection than a sphere.
Kylotan
A: 

For latitude, one degree is around 1852 meters.

The conversion for Longitude depends on the latitude. The math is a little fuzzy for me, but I seem to remember it being something like COS(latitude * PI / 180) * 1852

richardtallent
i think you mean 1852 km (i.e. 1,852,000 m)
Andreas F
Neither of those make sense. One *minute* of latitude is about 1852 metres (one nautical mile). One ° latitude is ~60 NM or ~111 km (~20000/180). 1852 km is 1000 NM, or ~16⅔°.
Ahruman
Doh! (slaps head) Ahruman is right.
richardtallent
Thanks for clearing it up, Ahruman. I simply knew the original value could not possibly be right, and assumed it was a problem with the unit, but never actually checked which unit.
Andreas F
+7  A: 

use - (CLLocationDistance)getDistanceFrom:(const CLLocation *)location

firstLongitude = 30.0;
firstLatitude = 30.0;

secondLongitude = 40.0;
secondLatitude = 40.0;

CLLocation *firstLoc = [[CLLocation alloc]initWithLatitude:firstLatitude longitude: firstLongitude];
CLLocation *secondLoc = [[CLLocation alloc]initWithLatitude:secondLatitude longitude: secondLongitude];

double distanceInMeters = [firstLoc getDistanceFrom: secondLoc];
oxigen
Voted up... I missed that this was an iPhone question. Using CoreLocation calls is definitely the way to go.
richardtallent
nice answer, but doesn't solve the problem: he still has to determine the point on the connecting line between A and B of which he wants to compute the distance from C, and that's the hard part
Christoph
@Christoph: I was gonna make a similar comment and also suggest (as you did) to solve the problem (or parts of it at least) in 3-space.
Andreas F
+4  A: 
Christoph
A: 

Here's Python code for solving your problem. It doesn't call any libraries so it could be ported quickly to any other language. Here's a derivation of the formula it uses.

John D. Cook
+1  A: 

As Christoph suggested, the harder part of the problem is probably better solved in 3-space.

My suggested solution (sorry, no code) - it assumes a spherical earth:

  • express A,B,C as vectors from earth center, E
  • find vectors orthogonal to both EA (vector from E to A) and EB (there should be two), via vector cross product
  • determine the smallest angle between the two orthogonal vectors and EC, via dot product
  • subtract from 90 deg
  • the resulting angle is the 'latitude' of C if we rotate the coordinate system such that A and B are on the equator, which can easily be converted to distance (see richardtallent's answer)
Andreas F