Skip to content

Instantly share code, notes, and snippets.

@didasy
Created July 31, 2017 19:18
Show Gist options
  • Select an option

  • Save didasy/74ae06a08e9669e85fc08ef976d6ccf0 to your computer and use it in GitHub Desktop.

Select an option

Save didasy/74ae06a08e9669e85fc08ef976d6ccf0 to your computer and use it in GitHub Desktop.
Calculate distance between two GPS point
// Calculate distance between two points
float distanceCoordinates(float flat1, float flon1, float flat2, float flon2) {
// Variables
float dist_calc=0;
float dist_calc2=0;
float diflat=0;
float diflon=0;
// Calculations
diflat = radians(flat2-flat1);
flat1 = radians(flat1);
flat2 = radians(flat2);
diflon = radians((flon2)-(flon1));
dist_calc = (sin(diflat/2.0)*sin(diflat/2.0));
dist_calc2 = cos(flat1);
dist_calc2*=cos(flat2);
dist_calc2*=sin(diflon/2.0);
dist_calc2*=sin(diflon/2.0);
dist_calc +=dist_calc2;
dist_calc=(2*atan2(sqrt(dist_calc),sqrt(1.0-dist_calc)));
dist_calc*=6371000.0; //Converting to meters
return dist_calc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment