Geolocation in HTML
? Geolocation in HTML (JavaScript API)
HTML itself doesn’t have a built-in geolocation tag, but modern browsers provide the Geolocation API through JavaScript to get the user's location.
What is Geolocation API?
Lets web apps get the user's geographic location (latitude, longitude)
Requires user permission to access location data
Works on most modern browsers and devices
Basic Example: Get User Location
<button onclick="getLocation()">Get Location</button><p id="output"></p><script> function getLocation() { const output = document.getElementById('output'); if (!navigator.geolocation) { output.textContent = 'Geolocation is not supported by your browser.'; return; } navigator.geolocation.getCurrentPosition(success, error); function success(position) { const latitude = position.coords.latitude; const longitude = position.coords.longitude; output.textContent = `Latitude: ${latitude} °, Longitude: ${longitude} °`; } function error() { output.textContent = 'Unable to retrieve your location.'; } }</script>How It Works
navigator.geolocation.getCurrentPosition()asks the user for permissionIf allowed, it returns the current coordinates
If denied or unavailable, it triggers an error callback
Use Cases
Show user location on a map
Provide location-based services or content
Track device movement (with continuous updates using
watchPosition())
Want me to show how to integrate geolocation with Google Maps or other mapping services?