Skip to content

Instantly share code, notes, and snippets.

@amrit3701
Last active January 22, 2018 17:52
Show Gist options
  • Select an option

  • Save amrit3701/73030fd7a1c253a7fdc8392c0fb05380 to your computer and use it in GitHub Desktop.

Select an option

Save amrit3701/73030fd7a1c253a7fdc8392c0fb05380 to your computer and use it in GitHub Desktop.
Get current user location
function getLocation() {
navigator.geolocation.getCurrentPosition(showPosition);
}
function showPosition(position) {
console.log("Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude);
}
getLocation();
/*
<Source>: https://www.w3schools.com/html/html5_geolocation.asp
=====
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
</body>
</html>
====
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment