Skip to content

Instantly share code, notes, and snippets.

@devdays
Created December 2, 2014 01:12
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save devdays/8ffaae0fb87d3c8fa9de to your computer and use it in GitHub Desktop.
Geolocation sample with watchPosition, clearWatch, error handling
<!DOCTYPE html>
<html>
<head>
<title>Location Test with Buttons</title>
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<script type="text/javascript">
window.addEventListener("load", function () {
var nav = null;
var watchID;
startWatching.addEventListener("click", function () {
if (nav == null) {
nav = window.navigator;
}
if (nav != null) {
var geoloc = nav.geolocation;
if (geoloc != null) {
watchID = geoloc.watchPosition(successCallback, errorCallback);
}
else {
console.log("Geolocation not supported");
}
}
else {
console.log("Navigator not found");
}
});
function successCallback(position) {
loc.innerHTML = "Latitude: " + position.coords.latitude +
" Longitude: " + position.coords.longitude;
}
function errorCallback(error) {
var message = "";
// Check for known errors
switch (error.code) {
case error.PERMISSION_DENIED:
message = "This website does not have permission to use " +
"the Geolocation API";
break;
case error.POSITION_UNAVAILABLE:
message = "The current position could not be determined.";
break;
case error.PERMISSION_DENIED_TIMEOUT:
message = "The current position could not be determined " +
"within the specified timeout period.";
break;
}
// If it's an unknown error, build a message that includes
// information that helps identify the situation so that
// the error handler can be updated.
if (message == "") {
var strErrorCode = error.code.toString();
message = "The position could not be determined due to " +
"an unknown error (Code: " + strErrorCode + ").";
}
console.log(message);
}
clearWatching.addEventListener("click", function () {
window.navigator.geolocation.clearWatch(watchID);
});
});
</script>
</head>
<body>
<div id="loc"></div>
<input id="startWatching" type="button" value="Watch Latitude and Longitude" />
<input id="clearWatching" type="button" value="Clear watch" />
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment