Here's a Gist showing how to convert from Degrees/Minutes/Seconds to decimal degrees.Below is the same code (in case GitHub is down)./*jslint browser: true, nomen: true */
/*jshint dojo, jquery, nomen:false */
/*global jQuery */
(function ($) {
"use strict";
// Matches DMS coordinates
// http://regexpal.com/?flags=gim®ex=^(-%3F\d%2B(%3F%3A\.\d%2B)%3F)[°%3Ad]%3F\s%3F(%3F%3A(\d%2B(%3F%3A\.\d%2B)%3F)['�?�%3A]%3F\s%3F(%3F%3A(\d%2B(%3F%3A\.\d%2B)%3F)["�?�]%3F)%3F)%3F\s%3F([NSEW])%3F&input=40%3A26%3A46N%2C79%3A56%3A55W 40%3A26%3A46.302N 79%3A56%3A55.903W 40°26�?�47�?�N 79°58�?�36�?�W 40d 26�?� 47�?� N 79d 58�?� 36�?� W 40.446195N 79.948862W 40.446195%2C -79.948862 40° 26.7717%2C -79° 56.93172
var dmsRe = /^(-?\d+(?:\.\d+)?)[°:d]?\s?(?:(\d+(?:\.\d+)?)['�?�:]?\s?(?:(\d+(?:\.\d+)?)["�?�]?)?)?\s?([NSEW])?/i;
// Results of match will be [full coords string, Degrees, minutes (if any), seconds (if any), hemisphere (if any)]
// E.g., ["40:26:46.302N", "40", "26", "46.302", "N"]
// E.g., ["40.446195N", "40.446195", undefined, undefined, "N"]
/** Parses a Degrees Minutes Seconds string into a Decimal Degrees number.
* @param {string} dmsStr A string containing a coordinate in either DMS or DD format.
* @return {Number} If dmsStr is a valid coordinate string, the value in decimal degrees will be returned. Otherwise NaN will be returned.
*/
function parseDms(dmsStr) {
var output = NaN, dmsMatch, degrees, minutes, seconds, hemisphere;
dmsMatch = dmsRe.exec(dmsStr);
if (dmsMatch) {
degrees = Number(dmsMatch[1]);
minutes = typeof (dmsMatch[2]) !== "undefined" ? Number(dmsMatch[2]) / 60 : 0;
seconds = typeof (dmsMatch[3]) !== "undefined" ? Number(dmsMatch[3]) / 3600 : 0;
hemisphere = dmsMatch[4] || null;
if (hemisphere !== null && /[SW]/i.test(hemisphere)) {
degrees = Math.abs(degrees) * -1;
}
if (degrees < 0) {
output = degrees - minutes - seconds;
} else {
output = degrees + minutes + seconds;
}
}
return output;
}
// Add coordinate validation method to jQuery validator.
if (typeof ($.validator) !== "undefined") {
$.validator.addMethod("coordinate", function (value, element) {
return this.optional(element) || dmsRe.test(value);
}, "Please enter a Decimal Degree or DMS value.");
}
$.parseDms = parseDms;
}(jQuery));