NMEA GLL to decimal degrees

This is filed under "things you don't find on stack overflow". Working with the sensors on the ships I found that my calculations I did to get the GPS location displayed in degrees, minutes and seconds was a bit off.

When the ship appears to be in the middel of Sweden, something is definitely wrong...

From the GPS I get coordinates like 6751.3425 N,01315.8982 E - that seems to be normal decimal degrees. Which is not true.

When dealing with NMEA GLL messages you have to do some more to the numbers.

The formula is to get the two numbers before the period including the period and the following numbers as one number. So 6751.3425 should be 51.3425. Take the numbers sans the one you've separated and add to the separated divided by 60.

Sounds complicated? Not so much. Using the latitude over it will be like this: 67+(51.3425/60) = 67,8557083333.

Lets do this in JavaScript.

getDms = function(latLon){
    var tempLatLon = latLon.toString();
    tempLatLon = tempValue.split('.');
    var tempLatLonLastTwo = tempLatLon[0].slice(-2)+'.'+ tempLatLon[1];
    tempLatLonLastTwo = (Number(tempLatLonLastTwo)*100)/100;
    var tempLatLonFirst = parseInt(tempLatLon[0].slice(0,-2));
    var initValue = tempLatLonFirst+(tempLatLonLastTwo/60);

This leave us with the latitude in decimal degrees. To finish the conversion to degrees/minute/seconds format the rest will look something like this.

var values = {};
    values.initValue = initValue;
    values.degrees = Math.abs(initValue);
    values.degreesInt = Math.floor(values.degrees);
    values.degreesFrac = values.degrees - values.degreesInt;
    values.secondsTotal = 3600 * values.degreesFrac;
    values.minutes = values.secondsTotal / 60;
    values.minutesInt = Math.floor(values.minutes);
    values.seconds = values.secondsTotal - (values.minutesInt * 60);
    var result = Math.floor(values.degrees)+'\u00B0'+Math.floor(values.minutes)+"'"; // To get seconds add +Math.floor(values.seconds)+'"'
    return result;

And all together as a function.

 /**
 * Transforms NMEA GLL til decimal degrees and then to degrees and minutes (and sec.)
 * Returns human readable GPS pos. in degrees and minutes.
 * @return string
 **/
 getDms = function(latLon){
    var tempLatLon = latLon.toString();
    tempLatLon = tempValue.split('.');
    var tempLatLonLastTwo = tempLatLon[0].slice(-2)+'.'+ tempLatLon[1];
    tempLatLonLastTwo = (Number(tempLatLonLastTwo)*100)/100;
    var tempLatLonFirst = parseInt(tempLatLon[0].slice(0,-2));
    var initValue = tempLatLonFirst+(tempLatLonLastTwo/60);
    var values = {};
    values.initValue = initValue;
    values.degrees = Math.abs(initValue);
    values.degreesInt = Math.floor(values.degrees);
    values.degreesFrac = values.degrees - values.degreesInt;
    values.secondsTotal = 3600 * values.degreesFrac;
    values.minutes = values.secondsTotal / 60;
    values.minutesInt = Math.floor(values.minutes);
    values.seconds = values.secondsTotal - (values.minutesInt * 60);
    var result = Math.floor(values.degrees)+'\u00B0'+Math.floor(values.minutes)+"'"; // To get seconds add +Math.floor(values.seconds)+'"'
    return result;
};

In this function I do not return the seconds, only the degrees and minutes.

I used some time to figure this one out. I post this so maybe someone else in need of this information can find it helpful!