0% found this document useful (0 votes)
3 views1 page

Geolocation Map Integration Script

The document is a JavaScript code snippet that tracks the user's geolocation when a button is clicked. It retrieves the user's latitude and longitude and displays them on the webpage, while also initializing a map with a marker at the user's location. Additionally, it handles various errors related to geolocation permissions and availability.

Uploaded by

duttaakash1411
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Geolocation Map Integration Script

The document is a JavaScript code snippet that tracks the user's geolocation when a button is clicked. It retrieves the user's latitude and longitude and displays them on the webpage, while also initializing a map with a marker at the user's location. Additionally, it handles various errors related to geolocation permissions and availability.

Uploaded by

duttaakash1411
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

[Link]('track-btn').

addEventListener('click', function() {
if ([Link]) {
[Link](showPosition, showError);
} else {
alert("Geolocation is not supported by this browser.");
}
});

function showPosition(position) {
const lat = [Link];
const lon = [Link];
[Link]('coords').innerHTML = `Latitude: ${lat}<br>Longitude: $
{lon}`;

// Initialize map
const map = [Link]('map').setView([lat, lon], 13);
[Link]('[Link] {
attribution: '&copy; <a
href="[Link] contributors'
}).addTo(map);

// Add marker
[Link]([lat, lon]).addTo(map)
.bindPopup('You are here!')
.openPopup();
}

function showError(error) {
switch([Link]) {
case error.PERMISSION_DENIED:
alert("User denied the request for Geolocation.");
break;
case error.POSITION_UNAVAILABLE:
alert("Location information is unavailable.");
break;
case [Link]:
alert("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
alert("An unknown error occurred.");
break;
}
}

You might also like