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;
}
Just change the text in brackets ([Lat/Long Cell]) to point to the DD cell you want to convert to DMS. If you want a different number of decimal places for the seconds add the desired number of 0's after the decimal point in the "00.00" part and also set the "2" to the number of places.
The key for knowing the difference is watching for the presence of a decimal in the coordinate string. If there is no decimal, it is DMS. If the decimal imme- diately follows the minutes coordinate (61° 34.25' or 61 34.25) then it's DM. If the decimal immediately follows the degrees coordinate (61.5708) then it's DD.
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:
toDegreesMinutesAndSeconds
function. That will return a string that will show, well, degrees, minutes, and seconds.
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..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With