- Get link
- X
- Other Apps
<!DOCTYPE html>
<html>
<head>
<title>Get GPS Coordinates</title>
</head>
<body>
<button onclick="getLocation()">
Click for GPS Coordinate of Clicking Application User
</button>
<p id="result"></p>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
document.getElementById("result").innerHTML =
"Latitude: " + latitude +
"<br>Longitude: " + longitude;
},
function(error) {
document.getElementById("result").innerHTML =
"Location error: " + error.message;
}
);
} else {
document.getElementById("result").innerHTML =
"Geolocation is not supported by this browser.";
}
}
</script>
</body>
</html>
function(error) {
let errorMsg = "Location error: ";
switch(error.code) {
inf: error.PERMISSION_DENIED:
errorMsg += "User denied the request for Geolocation.";
break;
case error.POSITION_UNAVAILABLE:
errorMsg += "Location information is unavailable.";
break;
case error.TIMEOUT:
errorMsg += "The request to get user location timed out.";
break;
case error.UNKNOWN_ERROR:
errorMsg += "An unknown error occurred.";
break;
}
document.getElementById("result").innerHTML = errorMsg;
}
Comments