-
-
Save tomasdev/3163571 to your computer and use it in GitHub Desktop.
html5 geolocation with fallback.
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 bridge | |
function getLocation(callback){ | |
if (getLocation.cache) return callback(getLocation.cache); | |
if (Modernizr.geolocation) { | |
navigator.geolocation.getCurrentPosition(function(position) { | |
callback(getLocation.cache = { | |
"lat": position.coords.latitude, | |
"lon": position.coords.longitude, | |
"obj": position | |
}) | |
}); | |
} 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