Skip to content

Instantly share code, notes, and snippets.

@jareware
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jareware/083ecc072bab29130415 to your computer and use it in GitHub Desktop.
Save jareware/083ecc072bab29130415 to your computer and use it in GitHub Desktop.
Converts the given string representation of WGS84 coordinates into a standard lat/lng object.
/**
* Converts the given string representation of WGS84 coordinates into a standard lat/lng object.
*
* @example 60 deg 08.2151' N, 24 deg 25.6136' E
* @example 43°38'19.39"N,116°14'28.86"W
* @example +43.6387194°, -116.2413500°
*
* For convenience (and to avoid "Cannot read property 'lat' of undefined" errors), always returns an
* object with the aforementioned keys. For invalid input, the VALUES of the keys will be undefined.
*
* @returns https://developers.google.com/maps/documentation/javascript/reference#LatLngLiteral
*
* @link https://gist.github.com/jareware/083ecc072bab29130415
* @author Jarno Rantanen <jarno@jrw.fi>
* @license Do whatever you want with it
*/
function parseWGS84String(input) {
return (typeof input === 'string' ? input : '0,0').replace(/deg/gi, '°').split(',').map(function(part) {
return (part.match(/([+-]?\d+(?:\.\d+)?\s*[°'"]|[nesw])+?/gi) || []).reduce(function(memo, component) {
var unit = component && component[component.length - 1].toUpperCase();
var power = '°\'"'.indexOf(unit);
if (power >= 0) {
return (memo || 0) + parseFloat(component) / Math.pow(60, power);
} else if ('SW'.indexOf(unit) >= 0) {
return (memo || 0) * -1;
}
return memo;
}, null);
}).reduce(function(memo, part, index) {
memo[index ? 'lng' : 'lat'] = typeof part === 'number' ? part : undefined;
return memo;
}, { lat: undefined, lng: undefined });
}
// UNIT TEST CODE:
[
// Input to function: // Expected output:
[ "60 deg 08.2151' N, 24 deg 25.6136' E", { lat: 60.136918333333334, lng: 24.426893333333332 } ],
[ "60° 08.2151' N, 24° 25.6136' E", { lat: 60.136918333333334, lng: 24.426893333333332 } ],
[ "43°38'19.39\"N, 116°14'28.86\"W", { lat: 43.63871944444445, lng: -116.2413500 } ],
[ "37° 26' 56.74\" N, 126° 27' 9.05\" E", { lat: 37.44909444444444, lng: 126.45251388888889 } ],
[ "+43.6387194°, -116.2413500°", { lat: 43.6387194, lng: -116.2413500 } ],
[ "0°, 0°", { lat: 0, lng: 0 } ],
[ "0, 0", { lat: undefined, lng: undefined } ],
[ "bla°, bla°", { lat: undefined, lng: undefined } ],
[ "bla", { lat: undefined, lng: undefined } ],
[ "", { lat: undefined, lng: undefined } ],
[ false, { lat: undefined, lng: undefined } ],
[ {}, { lat: undefined, lng: undefined } ],
// Oracle: http://www.earthpoint.us/convert.aspx
].forEach(function(tuple) {
var output = parseWGS84String(tuple[0]);
if (output.lat !== tuple[1].lat || output.lng !== tuple[1].lng) {
console.log('FAIL:', JSON.stringify({
input: tuple[0],
expected: tuple[1],
actual: output
}, undefined, 4));
} else {
console.log('OK:', tuple[0], '=>', output);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment