/ Published in: JavaScript
When you show the coordinates of a point, it’s sometimes better to show them as degrees and not as deciaml (even if decimal is simpler). Each of the two coordinates can be converted with the same function. In the function call the “tipo†is the type of the “v†value: if you call the function without the type, then the default type is “Nâ€, that means “NORTHâ€, it means that you’re converting a Latitude value. Values for latitude type are “N†for NORTH and “S†for SOUTH.
If you specify “E†or “W†than you’re converting a Longitude value.
If you specify “E†or “W†than you’re converting a Longitude value.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function convertDecDeg(v,tipo) { if (!tipo) tipo='N'; var deg; deg = v; if (!deg){ return ""; } else if (deg > 180 || deg < 0){ // convert coordinate from north to south or east to west if wrong tipo return convertDecDeg(-v,(tipo=='N'?'S': (tipo=='E'?'W':tipo) )); } else { var gpsdeg = parseInt(deg); var remainder = deg - (gpsdeg * 1.0); var gpsmin = remainder * 60.0; var D = gpsdeg; var M = parseInt(gpsmin); var remainder2 = gpsmin - (parseInt(gpsmin)*1.0); var S = parseInt(remainder2*60.0); return D+"° "+M+"' "+S+"'' "+tipo; } }