Created
February 14, 2014 14:24
-
-
Save cceremuga/9001819 to your computer and use it in GitHub Desktop.
Easy Native HTML5 Geolocation (no external library needed)
This file contains 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
function successCallback(position) { | |
//set latitude / longitude | |
var nearLat = position.coords.latitude; | |
var nearLong = position.coords.longitude; | |
//do whatever you need with the lat / long here | |
console.log('Latitude: ' + nearLat + ' Longitude: ' + nearLong); | |
} | |
function errorCallback(error) { | |
var errorMessage = ''; | |
switch (error.code) { | |
case error.PERMISSION_DENIED: | |
errorMessage = "<p>We cannot find your location. Please make sure location services is enabled then reload the page and click allow when prompted.</p>" | |
break; | |
case error.POSITION_UNAVAILABLE: | |
errorMessage = "<p>Location information is unavailable. Please try again later.</p>" | |
break; | |
case error.TIMEOUT: | |
errorMessage = "<p>The request to get your location timed out. Please try again later.</p>" | |
break; | |
case error.UNKNOWN_ERROR: | |
errorMessage = "<p>An unknown error occurred.</p>" | |
break; | |
} | |
//do something besides logging this to inform your user what has gone wrong | |
console.log(errorMessage); | |
} | |
//function to call initially when you want to query lat / long on the device, it calls successCallback on success, errorCallback on error | |
function getGeoDataFromDevice() { | |
//if geo is available, get the current position if the maximum age of the last retrieved position is older than 10 seconds | |
if (navigator.geolocation) { | |
//high accuracy gets a precise location, but utilizes more battery on a device. We're not doing this excessively, so you should be good to go. | |
//Maximum age is "how old can this data be, before we need something fresh" (10 seconds) | |
//Timeout is how long should we try to get this information before giving up (20 seconds) | |
navigator.geolocation.getCurrentPosition(successCallback, errorCallback, { enableHighAccuracy: true, maximumAge: 10000, timeout: 20000 }); | |
} else { | |
//do something with this besides logging to inform the user their device is unsupported | |
console.log('<p>Your device does not support location detection.</p>'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment