Skip to content

Instantly share code, notes, and snippets.

@candera
Created February 2, 2012 18:54
Show Gist options
  • Save candera/1725103 to your computer and use it in GitHub Desktop.
Save candera/1725103 to your computer and use it in GitHub Desktop.
Test of geolocation HTML
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function watchLocation(successCallback, errorCallback) {
successCallback = successCallback || function(){};
errorCallback = errorCallback || function(){};
// Try HTML5-spec geolocation.
var geolocation = navigator.geolocation;
if (geolocation) {
// We have a real geolocation service.
try {
function handleSuccess(position) {
successCallback(position.coords);
}
geolocation.watchPosition(handleSuccess, errorCallback, {
enableHighAccuracy: true,
maximumAge: 5000 // 5 sec.
});
} catch (err) {
errorCallback();
}
} else {
errorCallback();
}
}
function init() {
watchLocation(function(coords) {
document.getElementById('test').innerHTML = 'coords: ' + coords.latitude + ',' + coords.longitude;
}, function(e) {
document.getElementById('test').innerHTML = 'error:' + e.message;
});
}
</script>
</head>
<body onload="init()">
<div id="test">Loading...</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment