Created
September 1, 2018 05:03
-
-
Save srkiNZ84/c6493114a606f95f2d9be71f69cfb76a to your computer and use it in GitHub Desktop.
Simple Geolocation HTML example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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