Skip to content

Instantly share code, notes, and snippets.

@twolfson
Created November 11, 2012 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save twolfson/4053059 to your computer and use it in GitHub Desktop.
Save twolfson/4053059 to your computer and use it in GitHub Desktop.
geolocation lookup that accepts an error-first callback
// Asynchronous retrieval of coordinates
function getCoordinates(cb) {
// Grab geolocation from the navigator
var geolocation = window.navigator.geolocation;
// If geolocation exists, start fetching location
if (geolocation) {
geolocation.getCurrentPosition(function (position) {
cb(null, position.coords);
}, cb);
} else {
// Otherwise, callback with an error
cb(1);
}
}
$(function () {
// Grab the output and the geolocation
var $geoOutput = $('#geoOutput');
getCoordinates(function (err, coords) {
// If there was an error, update the text accordingly
if (err) {
$geoOutput.text('Geolocation could not be used =(');
} else {
// Otherwise, output the coordinates
$geoOutput.text('Geolocation found!! Lat: ' + coords.latitude + ' Lng: ' + coords.longitude);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment