Created
April 26, 2020 21:27
-
-
Save betweenbrain/e571bb6cf1f845f61349080d2054680f to your computer and use it in GitHub Desktop.
Geolocation Test Page (from https://stackoverflow.com/a/61331036/901680)
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
<!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, showError); | |
} else { | |
x.innerHTML = "Geolocation is not supported by this browser."; | |
} | |
} | |
function showPosition(position) { | |
x.innerHTML = "Latitude: " + position.coords.latitude + | |
"<br>Longitude: " + position.coords.longitude; | |
} | |
function showError(error) { | |
switch(error.code) { | |
case error.PERMISSION_DENIED: | |
x.innerHTML = "User denied the request for Geolocation." | |
break; | |
case error.POSITION_UNAVAILABLE: | |
x.innerHTML = "Location information is unavailable." | |
break; | |
case error.TIMEOUT: | |
x.innerHTML = "The request to get user location timed out." | |
break; | |
case error.UNKNOWN_ERROR: | |
x.innerHTML = "An unknown error occurred." | |
break; | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment