Created
July 17, 2013 12:11
-
-
Save jboulhous/6020008 to your computer and use it in GitHub Desktop.
html5 geolocation with fallback
// https://gist.github.com/CaioBianchi/1990860
This file contains hidden or 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
// 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