Rolando
Rolando

Reputation: 62626

How to convert lat long from decimal degrees to DMS format?

Assuming I have a latitude longitude: 38.898556, -77.037852. How do I convert this to DMS?

Expected output is:

38 53 55 N
77 2 16 W

Want to be able to accept both a latitude and longitude as input parameters in the function.

Current function is as follows:

function convertDMS( lat, lng ) {

        var convertLat = Math.abs(lat);
        var LatDeg = Math.floor(convertLat);
        var LatMin = (Math.floor((convertLat - LatDeg) * 60));
        var LatCardinal = ((lat > 0) ? "n" : "s");

        var convertLng = Math.abs(lng);
        var LngDeg = Math.floor(convertLng);
        var LngMin = (Math.floor((convertLng - LngDeg) * 60));
        var LngCardinal = ((lng > 0) ? "e" : "w");

        return LatDeg + LatCardinal + LatMin  + "    " + LngDeg + LngCardinal + LngMin;
}

Upvotes: 19

Views: 21706

Answers (5)

whatistruth
whatistruth

Reputation: 139

convert LAT & LNG to DMS

function toDMS(deg) {
    var d = Math.floor(deg);
    var min = Math.floor((deg - d) * 60);
    var sec = ((deg - d - min / 60) * 3600).toFixed(2);
    return d + "°" + min + "'" + sec + "\"";
}

function convertLatLngToDMS(lat, lng) {
    var latDMS = lat >= 0 ? "N" : "S";
    var lngDMS = lng >= 0 ? "E" : "W";
    
    lat = Math.abs(lat);
    lng = Math.abs(lng);
    
    var latDMSString = toDMS(lat);
    var lngDMSString = toDMS(lng);
    
    return latDMSString + " " + latDMS + " " + lngDMSString + " " + lngDMS;
}

USAGE:

const lat = 64.75960;
const lng = -104.21007;

const dms = convertLatLngToDMS(lat, lng)
console.log(dms); // 64°45'34.56" N 104°12'36.25" W

Upvotes: -1

Mohsen Alyafei
Mohsen Alyafei

Reputation: 5547

Here is a shorter solution to convert lat long from decimal degrees to the DMS format:

I have used the separator "/" between the lat and long, but that can be removed if you prefer.

function toDMS(lat,long) {
const toDMS=coord=>{min=~~(minA=((a=Math.abs(coord))-(deg=~~a))*60);
return deg+"° "+min+"' "+Math.ceil((minA-min)*60)+'"';
};
return `${lat>=0?"N":"S"} ${toDMS(lat)} / ${long>=0?"E":"W"} ${toDMS(long)}`;
}

// examples
console.log(toDMS( 22.22222, 11.11111));
console.log(toDMS( -22.22222, -11.11111));

Upvotes: 0

Marco Tibben
Marco Tibben

Reputation: 71

here's two simple functions i created for this; just give the dms to the script

function ConvertDMSToDEG(dms) {   
    var dms_Array = dms.split(/[^\d\w\.]+/); 
    var degrees = dms_Array[0];
    var minutes = dms_Array[1];
    var seconds = dms_Array[2];
    var direction = dms_Array[3];

    var deg = (Number(degrees) + Number(minutes)/60 + Number(seconds)/3600).toFixed(6);

    if (direction == "S" || direction == "W") {
        deg = deg * -1;
    } // Don't do anything for N or E
    return deg;
}

and visa versa just give the degrees to the script, and true of false for lat (latitude)

function ConvertDEGToDMS(deg, lat) {
    var absolute = Math.abs(deg);

    var degrees = Math.floor(absolute);
    var minutesNotTruncated = (absolute - degrees) * 60;
    var minutes = Math.floor(minutesNotTruncated);
    var seconds = ((minutesNotTruncated - minutes) * 60).toFixed(2);

    if (lat) {
        var direction = deg >= 0 ? "N" : "S";
    } else {
        var direction = deg >= 0 ? "E" : "W";
    }

    return degrees + "°" + minutes + "'" + seconds + "\"" + direction;
}

hope this helps people..

Upvotes: 4

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

I decided to simplify your math and do things in separate steps. I'm one degree off from your answer, so I'm going to chalk that up to a rounding issue -- I don't know the exact rules to do the convert.

var src = [38.898556, -77.037852];


// 38 53 55 N
// 77 2 16 W

function convertToDMS(src) {
  function toDMS(n) {
    // The sign doesn't matter
    n = Math.abs(n);

    // Get the degrees
    var d = Math.floor(n);
    // Strip off the answer we already have
    n = n - d;
    // And then put the minutes before the '.'
    n *= 60;
    
    // Get the minutes
    var m = Math.floor(n);
    // Remove them from the answer
    n = n - m;
    // Put the seconds before the '.'
    n *= 60;
        
    // Get the seconds
    // Should this be round? Or rounded by special rules?
    var s = Math.floor(n);

    // Put it together.
    return "" + d + " " + m + " " + s;

  }

  var dir0 = src[0] > 0 ? "N" : "S";
  
  var dir1 = src[1] > 0 ? "E" : "W";
  
  
  
  
  console.log(toDMS(src[0]) + dir0);
  console.log(toDMS(src[1]) + dir1);

}


convertToDMS(src);

Upvotes: 0

Alpha
Alpha

Reputation: 7858

function toDegreesMinutesAndSeconds(coordinate) {
    var absolute = Math.abs(coordinate);
    var degrees = Math.floor(absolute);
    var minutesNotTruncated = (absolute - degrees) * 60;
    var minutes = Math.floor(minutesNotTruncated);
    var seconds = Math.floor((minutesNotTruncated - minutes) * 60);

    return degrees + " " + minutes + " " + seconds;
}

function convertDMS(lat, lng) {
    var latitude = toDegreesMinutesAndSeconds(lat);
    var latitudeCardinal = lat >= 0 ? "N" : "S";

    var longitude = toDegreesMinutesAndSeconds(lng);
    var longitudeCardinal = lng >= 0 ? "E" : "W";

    return latitude + " " + latitudeCardinal + "\n" + longitude + " " + longitudeCardinal;
}

Here's an explanation on how this code works:

  • The processing method for the latitude and longitude is pretty much the same, so I abstracted that out to the toDegreesMinutesAndSeconds function. That will return a string that will show, well, degrees, minutes, and seconds.
    • This function will start with the coordinate and truncate it. This value, in positive, is your amount of degrees.
    • The decimal portion needs to be converted to minutes. We take what's left from that rounding and we multiply it by 60.
    • We apply the same logic to get the seconds: so we use only the truncated number for our string but we keep the non-truncated to get the decimal part.
  • Finally, we check if the original value of the coordinate was positive or negative. For latitude, positive (or zero) is North, otherwise South. For longitude, positive (or zero) is East, otherwise, West.

Upvotes: 51

Related Questions