Skip to content

Instantly share code, notes, and snippets.

@CamonZ
Forked from paulirish/gist:366184
Created July 11, 2010 14:37
Show Gist options
  • Save CamonZ/471593 to your computer and use it in GitHub Desktop.
Save CamonZ/471593 to your computer and use it in GitHub Desktop.
// geo-location bridge
function getLocation(callback){
if (getLocation.cache) return callback(getLocation.cache);
if (Modernizr.geolocation) {
var geocallback = function(position) {
callback(getLocation.cache = {
"lat": position.coords.latitude,
"lon": position.coords.longitude,
"obj": position
})
};
navigator.geolocation.getCurrentPosition(geocallback);
navigator.geolocation.watchCurrentPosition(geocallback);
} else {
$.getScript('//www.google.com/jsapi',function(){
callback(getLocation.cache = {
"lat": google.loader.ClientLocation.latitude,
"lon": google.loader.ClientLocation.longitude,
"obj": google.loader.ClientLocation
})
});
}
}
// usage
getLocation(function(pos){
console.log("I'm located at ",pos.lat,' and ',pos.lon);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment