Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Get Location Information
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=content-type content="text/html;charset=UTF-8">
<title>GeoLocation Diagnostics</title>
<meta name=viewport content="width=device-width,initial-scale=1,maximum-scale=1">
<meta name=format-detection content="telephone=no">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<div class=container>
<table class=table>
<caption>GeoLocation Diagnostics</caption>
<tbody id=Data></tbody>
</table>
<script>
var table = $('#Data'),
row = function(k, v) {
return $('<tr><td>' + k + '</td><td><code>' + v + '</code></td></tr>');
};
if ("geolocation" in navigator) {
table.append(row('"geolocation" in navigator', true));
if (navigator.geolocation.getCurrentPosition) {
table.append(row('navigator.geolocation.getCurrentPosition', 'function()'));
}
navigator.geolocation.getCurrentPosition(function(location) {
var start = {
lat: location.coords.latitude,
lng: location.coords.longitude
}
for (var key in location.coords) {
table.append(row('1st location.coords.' + key, location.coords[key]));
}
table.append(row('waiting 3 seconds', 'started'));
setTimeout(function() {
table.append(row('waiting 3 seconds', 'completed'));
navigator.geolocation.getCurrentPosition(function(location) {
for (var key in location.coords) {
table.append(row('2nd location.coords.' + key, location.coords[key]));
}
table.append(row('diff location.coords.latitude', start.lat - location.coords.latitude));
table.append(row('diff location.coords.longitude', start.lng - location.coords.longitude));
}, function() {
table.append(row('navigator.geolocation.getCurrentPosition 2nd', 'failed'));
}, {
enableHighAccuracy: true,
maximumAge: 0
});
}, 3000);
}, function() {
table.append(row('navigator.geolocation.getCurrentPosition 1st', 'failed'));
}, {
enableHighAccuracy: true,
maximumAge: 0
});
} else {
table.append(row('"geolocation" in navigator', false));
}
</script>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment