Go Down

Topic: Expanding TinyGPS Library with distance function (Read 1 time) previous topic - next topic

Philipp Maricek


I want to expand the TinyGPS Library with a "distance and course to the next way point" function. I have never wrote my own Header file before, so I don't know what to do. I already took a look into the file and also in the cpp file. What do I have to do for adding a new function?


Philipp Maricek

Jun 29, 2010, 11:53 am Last Edit: Jun 29, 2010, 01:03 pm by lord-maricek Reason: 1

I just tryed it, and it seems to work. I wrote this into the TinyGPS.h:
Code: [Select]
void get_distance(float lat1, float lat2, float lon1, float lon2, float *course_, float *distance){
       float lat_dif;
       float lon_dif;

       *distance = sin(lat1/180.0*PI)*sin(lat2/180.0*PI) + cos(lat1/180.0*PI)*cos(lat2/180.0*PI)*cos((lon2/180.0*PI)-(lon1/180.0*PI));
       *distance = acos(*distance);
       *distance *= 6378.137*1000;

       lat_dif = lat1 - lat2;
           lat_dif *= (-1.0);
       lon_dif = lon1 - lon2;
           lon_dif *= (-1.0);

       lat_dif *= 60.0 * 1852.0;

       lon_dif *= 60.0;
       lon_dif *= cos(lat2*PI/180);
       lon_dif *= 1852.0;

       *course_ = atan(lon_dif/lat_dif)*180/PI;

           *course_ = 180.0 - *course_;
       }else if((lat2<lat1)&&(lon2<lon1)){
           *course_ = *course_ + 180.0;
       }else if((lat2>lat1)&&(lon2<lon1)){
           *course_ = 360.0 - *course_;


Antonio Santos

Thanks for posting this here, Philipp! Great stuff! Works great!


Antonio Santos


Philipp Maricek:

Philipp - how accurate is that code, by the way? Can it handle over-the-horizon navigation (earth's curvature and all that)? I am just curious, as such code could be useful to me in the future...
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131