# Expanding TinyGPS Library with distance function

Hi,

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?

Greetings
Philipp

Hi,

I just tryed it, and it seems to work. I wrote this into the TinyGPS.h:

``````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;
if(lat_dif<0){
lat_dif *= (-1.0);
}
lon_dif = lon1 - lon2;
if(lon_dif<0){
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;

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

Philipp

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

Cheers,

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