Skip to content

Instantly share code, notes, and snippets.

@srkiNZ84
Created September 1, 2018 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srkiNZ84/c6493114a606f95f2d9be71f69cfb76a to your computer and use it in GitHub Desktop.
Save srkiNZ84/c6493114a606f95f2d9be71f69cfb76a to your computer and use it in GitHub Desktop.
Simple Geolocation HTML example
<html>
<head>
<title>Geolocation</title>
</head>
<body>
<script>
if ("geolocation" in navigator){
console.log("Geolocation available");
navigator.geolocation.getCurrentPosition(success, error);
}
else {
console.log("Geolocation NOT available");
}
function success(pos) {
var crd = pos.coords;
var lat = document.getElementById("latitude");
var longitude = document.getElementById("longitude");
var acc = document.getElementById("accuracy");
console.log('Your current position is:');
console.log(`Latitude : ${crd.latitude}`);
lat.innerHTML = crd.latitude;
console.log(`Longitude: ${crd.longitude}`);
longitude.innerHTML = crd.longitude;
console.log(`More or less ${crd.accuracy} meters.`);
acc.innerHTML = crd.accuracy;
}
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
}
</script>
<p>
Hi there! Your location is:<br />
Latitude: <div id="latitude"></div><br />
Longitude: <div id="longitude"></div><br />
Accuracy: <div id="accuracy"></div><br />
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment