Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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