Skip to content

Instantly share code, notes, and snippets.

@dsaiztc
Created June 7, 2014 10:27
Show Gist options
  • Save dsaiztc/4a1c6ebcbb1d1f978f7d to your computer and use it in GitHub Desktop.
Save dsaiztc/4a1c6ebcbb1d1f978f7d to your computer and use it in GitHub Desktop.
Converts NMEA GPS format to grades.
/**
* Converts a latitude/longitude in NMEA format to grades (4322.0399N to 43.36715)
* @param lat_or_long latitude/longitude in NMEA format
* @return GPS latitude/longitude in grades
*/
public static float convertLatLon(String lat_or_long)
{
int number_of_decimals = 4; // Número de decimales de los grados
String minute = lat_or_long.substring(lat_or_long.length() - (4 + number_of_decimals), lat_or_long.length() - 1);
float minutef = Float.parseFloat(minute);
float minutef_grade = minutef / 60;
String grade = lat_or_long.substring(0, lat_or_long.length() - (4 + number_of_decimals));
float gradef = Float.parseFloat(grade);
gradef += minutef_grade;
char orientation = lat_or_long.charAt(lat_or_long.length() - 1);
int positive = 0;
switch (orientation)
{
case 'N':
case 'E':
positive = +1;
break;
case 'S':
case 'W':
positive = -1;
break;
}
gradef *= positive;
return gradef;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment