Created
August 22, 2014 03:31
-
-
Save mbykovskyy/1c67b0b4ba8da9972488 to your computer and use it in GitHub Desktop.
Convert decimal degrees to degrees minutes seconds.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts decimal degrees to degrees minutes seconds. | |
* | |
* @param dd the decimal degrees value. | |
* @param isLng specifies whether the decimal degrees value is a longitude. | |
* @return degrees minutes seconds string in the format 49°15'51.35"N | |
*/ | |
function convertToDms(dd, isLng) { | |
var dir = dd < 0 | |
? isLng ? 'W' : 'S' | |
: isLng ? 'E' : 'N'; | |
var absDd = Math.abs(dd); | |
var deg = absDd | 0; | |
var frac = absDd - deg; | |
var min = (frac * 60) | 0; | |
var sec = frac * 3600 - min * 60; | |
// Round it to 2 decimal points. | |
sec = Math.round(sec * 100) / 100; | |
return deg + "°" + min + "'" + sec + '"' + dir; | |
} |
BUG in this code: convertToDms(36.183333333333, true) = 36°10'60"E
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
מה אומר קוד זה
var dir = dd < 0
? isLng ? 'W' : 'S'
: isLng ? 'E' : 'N';