Skip to content

Instantly share code, notes, and snippets.

@appuchias
Created May 20, 2024 23:58
Show Gist options
  • Save appuchias/19c4cdce769fcbaed7abba7bac1ed1e3 to your computer and use it in GitHub Desktop.
Save appuchias/19c4cdce769fcbaed7abba7bac1ed1e3 to your computer and use it in GitHub Desktop.
Coordinates JS API base sample file
<!DOCTYPE html>
<html lang="en">
<head>
<title>Geolocation API</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
// Check if Geolocation is supported
if ("geolocation" in navigator) {
// Request the user's permission to access their location
navigator.geolocation.getCurrentPosition(
function(position) {
// If permission is granted, retrieve the user's coordinates
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
console.log("(Lat., Lon.): (" + latitude + ", " + longitude + ")");
},
function(error) {
// Handle errors
console.error("Error getting location: ", error);
}
);
} else {
// Geolocation is not supported
console.log("Geolocation is not supported by this browser.");
}
</script>
</head>
<body>
<div id="coords"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment