Skip to content

Instantly share code, notes, and snippets.

@bmcmurray
Forked from paulirish/gist:366184
Created October 12, 2011 02:01
Show Gist options
  • Save bmcmurray/1280024 to your computer and use it in GitHub Desktop.
Save bmcmurray/1280024 to your computer and use it in GitHub Desktop.
html5 geolocation with fallback.
// geo-location shim
// currentely only serves lat/long
// depends on jQuery
;(function(geolocation){
if (geolocation) return;
var cache;
geolocation = window.navigator.geolocation = {};
geolocation.getCurrentPosition = function(callback){
if (cache) callback(cache);
$.getScript('//www.google.com/jsapi',function(){
cache = {
coords : {
"latitude": google.loader.ClientLocation.latitude,
"longitude": google.loader.ClientLocation.longitude
}
};
callback(cache);
});
};
geolocation.watchPosition = geolocation.getCurrentPosition;
})(navigator.geolocation);
// usage
navigator.geolocation.watchPosition(function(pos){
console.log("I'm located at ",pos.coords.latitude,' and ',pos.coords.longitude);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment