# GPS formulas

I am trying to write up a code using the known GPS position to caluate the distance to a given point>

long  distance  =  dist2Point(long latitudeOfPoint,long longitudeOfPoint)

and also distance to a given line which goes between to GPS points>

long  distance2Line  =  dist2Point(long latitudeOfPointStart,long longitudeOfPointStart, long latitudeOfPointEnd,long longitudeOfPointEnd)

any ideas how to do this by using snippets of existing code?

Thanks for all ideas

Views: 347

### Replies to This Discussion

long get_distance(struct Location *loc1, struct Location *loc2)
{
if(loc1->lat == 0 || loc1->lng == 0)
return -1;
if(loc2->lat == 0 || loc2->lng == 0)
return -1;
float dlat   = (float)(loc2->lat - loc1->lat);
float dlong  = ((float)(loc2->lng - loc1->lng)) * scaleLongDown;
return sqrt(sq(dlat) + sq(dlong)) * .01113195;
}

So its just to multiple it with * .01113195; to get meter? Great. so that is it?

Kim

No! You have to scale the longitude as you move north/south from the equator. See the factor scaleLongDown. It is calculated as:

// this is used to offset the shrinking longitude as we go towards the poles
float rads = (abs(next_WP.lat)/t7) * 0.0174532925;
I find this site full of useful info.

1

2

3

4

5

6

7

8

9

10

## Groups

1068 members

39 members

22 members

41 members

• ### ArduRover User Group

602 members

Season Two of the Trust Time Trial (T3) Contest
A list of all T3 contests is here. The current round, the Vertical Horizontal one, is here