-
Notifications
You must be signed in to change notification settings - Fork 0
/
6-Geolocation.html
26 lines (24 loc) · 939 Bytes
/
6-Geolocation.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<h3>Getting user current location...</h3>
<script>
/*
a. Implement a JavaScript function that retrieves the user's current location (latitude and longitude) using the geolocation API.
*/
function getCurrentLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function (position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
document.write(`Latitude: ${latitude}<br/>`);
document.write(`Longitude: ${longitude}`);
},
function (error) {
document.write(`Error retrieving location: ${error.message}`);
}
);
} else {
document.write('Geolocation is not supported by this browser.');
}
}
getCurrentLocation();
</script>