Skip to content

Instantly share code, notes, and snippets.

@iwek
Created March 18, 2013 18:43
Show Gist options
  • Save iwek/5189674 to your computer and use it in GitHub Desktop.
Save iwek/5189674 to your computer and use it in GitHub Desktop.
JavaScript Geographic Coordinate Conversion
function convert(geo){
var latlonrg = /(\d+(?:\.\d+)?)[\xb0\s]?\s*(?:(\d+(?:\.\d+)?)['\u2019\u2032\s])?\s*(?:(\d+(?:\.\d+)?)["\u201d\u2033\s])?\s*([SNEW])?/i;
var m = String(geo).split(latlonrg),
lat = m && +m[1] + (m[2] || 0) / 60 + (m[3] || 0) / 3600;
if (m[4].toUpperCase() == "S") {
lat = -lat;
}
var lon = m && +m[6] + (m[7] || 0) / 60 + (m[8] || 0) / 3600;
if (m[9].toUpperCase() == "W") {
lon = -lon;
}
return lat+','+lon;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment