Geolocation API in JavaScript
Read latitude and longitude in JavaScript with getCurrentPosition on HTTPS or localhost. A button starts the request; refusal and timeout errors are handled.
Save this as index.html and serve it on HTTPS or localhost. Localhost is a web server on your own computer. Use the page buttons to request access.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<button id="locate">Find my location</button><p id="status">Location not requested.</p>7<script>8const status = document.querySelector('#status');9document.querySelector('#locate').onclick = () => {10 if (!navigator.geolocation) { status.textContent = 'Location is not supported.'; return; }11 status.textContent = 'Waiting for permission and a position...';12 navigator.geolocation.getCurrentPosition(13 ({ coords }) => { status.textContent = 'Latitude: ' + coords.latitude.toFixed(4) + ', Longitude: ' + coords.longitude.toFixed(4); },14 error => { status.textContent = error.code === 1 ? 'Location permission denied.' : 'Could not get a location. Try again outdoors.'; },15 { timeout: 10000, maximumAge: 60000 }16 );17};18</script>19</body>20</html>
Sample Output
Latitude: 28.6139, Longitude: 77.2090
A possible success result after permission. Your actual coordinates can differ.
Location permission denied.
The error callback handles a refusal; it is not a location result.
In this one
- Ask for location after a click
- Handle success and refusal separately
- Read and round coordinates
- Set a waiting limit
How it works
- 1
Find the button and status paragraph.
- 2
Check for navigator.geolocation before calling it.
- 3
Show a waiting message, then request a position with a ten-second timeout.
- 4
Round the returned coordinates for display.
- 5
Compare the sample with your own result; refusal uses a different message.
Edge cases
Permission refused
The person selects Block.
The error callback shows a refusal message. Do not keep asking on every click; explain how site settings work if the person wants to try again.
Insecure page or weak signal
Location can fail even when the API exists.
Use HTTPS or localhost. A timeout limits waiting, but it cannot create a GPS signal or force an accurate result.
What's going on
The Geolocation API asks the browser for the device's position. An API is a set of features your code can call. This can help a campus map find nearby buildings, but the person must agree to share their location.
Open this browser-only example on HTTPS or localhost. Localhost means a web server running on your own computer. The button starts the request; the success function receives latitude and longitude. These numbers describe a position north or south and east or west on Earth.
The answer arrives later, so the waiting message appears first. The error function handles refusal or failure instead of leaving the page stuck. The displayed coordinates are only a sample. Your result depends on the device and may be approximate. Do not store or publish a person's location without a clear reason and consent.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What happens after Block is selected?
Pick an answer to check it.
Question 2
What does timeout: 10000 limit?
Pick an answer to check it.
Questions people ask
Will everyone see the sample coordinates?
No. Those numbers are an example. The browser returns the current device location, or an error.
Does this read the location immediately?
No. The browser may ask for permission and then wait for a location provider.
Why round to four decimal places?
It keeps the page readable. It does not improve the accuracy of the measured position.