Javascript decimal to degrees for google maps


/ Published in: JavaScript
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. function convertDecDeg(v,tipo) {
  2. if (!tipo) tipo='N';
  3. var deg;
  4. deg = v;
  5. if (!deg){
  6. return "";
  7. } else if (deg > 180 || deg < 0){
  8. // convert coordinate from north to south or east to west if wrong tipo
  9. return convertDecDeg(-v,(tipo=='N'?'S': (tipo=='E'?'W':tipo) ));
  10. } else {
  11. var gpsdeg = parseInt(deg);
  12. var remainder = deg - (gpsdeg * 1.0);
  13. var gpsmin = remainder * 60.0;
  14. var D = gpsdeg;
  15. var M = parseInt(gpsmin);
  16. var remainder2 = gpsmin - (parseInt(gpsmin)*1.0);
  17. var S = parseInt(remainder2*60.0);
  18. return D+"&deg; "+M+"' "+S+"'' "+tipo;
  19. }
  20. }

URL: http://www.barattalo.it/2009/12/26/decimal-degrees-conversion-and-distance-of-two-points-on-google-map/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.