Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Geolocation error function
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,
errorFunction);
} else {
loc.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
loc.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
function errorFunction(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
loc.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
loc.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
loc.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
loc.innerHTML = "An unknown error occurred."
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment