# Posted By

ginoplusio on 12/26/09

# Statistics

Viewed 550 times
Favorited by 1 user(s)

# Javascript decimal to degrees for google maps

/ 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.

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+"&deg; "+M+"' "+S+"'' "+tipo;	}}` Subscribe to comments