Suggested derived functions:
function toStringHDM(coordinate) {
if (coordinate) {
return (degreesToStringHDM('NS', coordinate[1]) +
' ' +
degreesToStringHDM('EW', coordinate[0]));
}
else {
return '';
}
}
and:
function degreesToStringHDM(hemispheres, degrees, opt_fractionDigits = 0) {
var normalizedDegrees = modulo(degrees + 180, 360) - 180;
var x = Math.abs(3600 * normalizedDegrees);
var dflPrecision = opt_fractionDigits || 0;
var precision = Math.pow(10, dflPrecision);
var deg = Math.floor(x / 3600);
var min = Math.floor((x - deg * 3600) / 60);
var sec = x - deg * 3600 - min * 60;
sec = Math.ceil(sec * precision) / precision;
if (sec >= 60) {
sec = 0;
min += 1;
}
if (min >= 60) {
min = 0;
deg += 1;
}
return (deg +
'\u00b0 ' +
padNumber(min, 2) +
'\u2032 ' +
(normalizedDegrees == 0
? ''
: ' ' + hemispheres.charAt(normalizedDegrees < 0 ? 1 : 0)));
}
Please note: assuming the compiler will do the job for me, I intentionally left a lot of dead code inside degreesToStringHDM() in order to keep it as close as possible to OpenLayers' original function degreesToStringHDMS(), should you need to compare both in the future (for example, when OpenLayers will be releasing a new version of this function, potentially fixing some bugs, you may be interested to perform a diff in order to quickly find what needs to be fixed here...)