Skip to content

Instantly share code, notes, and snippets.

@brunocruzcoelho
Created January 24, 2014 10:57
Show Gist options
  • Save brunocruzcoelho/8595391 to your computer and use it in GitHub Desktop.
Save brunocruzcoelho/8595391 to your computer and use it in GitHub Desktop.
html - get location
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Geolocation accuracy</title>
<script type="text/javascript">
// cache for changes
var lastPosition = null;
function initialize() {
if(navigator.geolocation) {
watch = navigator.geolocation.watchPosition(updatePosition,error_callback, {enableHighAccuracy:true, maximumAge:1000});
}
}
function updatePosition(p) {
// no movement
if(lastPosition && lastPosition.coords.latitude == p.coords.latitude && lastPosition.coords.longitude == p.coords.longitude) {
return;
}
// set position
lastPosition = p;
var date = new Date();
document.getElementById('target').innerHTML+= date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds() + ': lat=' + p.coords.latitude.toFixed(6) + ';lon=' + p.coords.longitude.toFixed(6) + ' | ' + p.coords.accuracy + '<br />';
}
function error_callback(p) {
// do nothing
}
</script>
</head>
<body onload="initialize();">
loc:<p id="target"></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment