Skip to content

Instantly share code, notes, and snippets.

@genejones
Last active December 16, 2015 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genejones/5490153 to your computer and use it in GitHub Desktop.
Save genejones/5490153 to your computer and use it in GitHub Desktop.
Determine current location using HTML5 techniques.
//made with help from https://developer.mozilla.org/en-US/docs/DOM/window.navigator.geolocation.getCurrentPosition
function setPositionFromCurrent(){
//this requires HTML5
if (!!navigator.geolocation){
navigator.geolocation.getCurrentPosition(function (position){
var coords = position.coords;
console.log('Latitude : ' + coords.latitude);
console.log('Longitude: ' + coords.longitude);
window.lat = coords.latitude;
window.lon = coords.longitude;
}, function (error){alert("Couldn't set geo-location automatically due to the following: "+error.message);},
{enableHighAccuracy:true, timeout: 5000, maximumAge: 30}
//if the device has GPS or another high accuracy device, use that
//but don't take more than 5seconds to do so
//cached locations are OK, but they must be less than 30 seconds old
);
}
else{
alert("Your browser does not support geolocation. Try again with a new version of Chrome 5+, Firefox 3.5+, Internet Explorer 9+, Safari 5+, or Opera 10.60+");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment