Created
November 11, 2012 00:05
-
-
Save twolfson/4053059 to your computer and use it in GitHub Desktop.
geolocation lookup that accepts an error-first callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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