The βGeolocation APIβ helps websites find a user's location with their permission. It is often used in navigation, maps and location-based services. ππ
In simple words, HTML5 Geolocation is a browser API that is used for getting the device geographic position in the form of latitude, longitude and accuracy coordinates. This can help detect the geolocation of the visitor or user on a website or app.
SYNTAX
{
navigator.geolocation.getCurrentPosition(successCallback, errorCallback)
}
π Geolocation Methods:
Method | Description |
---|---|
getCurrentPosition() | Retrieve current geographic location website user |
watchPosition() | Update the user's live location continuously |
clearWatch() | Clear the ongoing watch of the user's location |
-
How does it workβπ€π
- Uses GPS, Wi-Fi & IP address to get the location
- Always returns latitude, longitude and accuracy properties
- Other properties are returned if available
- Asks for user permission for privacy and security reasons
Handling Success Response
π Success: Returns position object containing latitude & longitude
function showPosition(position){
var x = `Your current location is (Latitude: ${position.coords.latitude}, Longtitude: ${position.coords.longitude})`;
document.getElementById("location").innerHTML = x;
}
- Handling Errors
β οΈError: Handles problems like permission denied or timeout
function showError(error) {
switch(error.code){
case error.PERMISSION_DENIED:
alert("User denied the request for Geolocation API.");
break;
case error.POSITION_UNAVAILABLE:
alert("USer location information is unavailable.");
break;
case error.TIMEOUT:
alert("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
alert("An unknown error occurred.");
break;
}
}
COMPLETE EXAMPLE OF GEOLOCATION API
<!DOCTYPE html>
<html>
<head>
<title>Geolocation API</title>
</head>
<body>
<h1>Find your Current location</h1>
<button onclick="getlocation()">Click me</button>
<div id="location"></div>
<script>
var x= document.getElementById("location");
function getlocation() {
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition, showError)
}
else
{
alert("Sorry! your browser is not supporting")
} }
function showPosition(position){
var x = `Your current location is (Latitude: ${position.coords.latitude}, Longtitude: ${position.coords.longitude})`;
document.getElementById("location").innerHTML = x;
}
function showError(error) {
switch(error.code){
case error.PERMISSION_DENIED:
alert("User denied the request for Geolocation API.");
break;
case error.POSITION_UNAVAILABLE:
alert("USer location information is unavailable.");
break;
case error.TIMEOUT:
alert("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
alert("An unknown error occurred.");
break;
}
}
</script>
</body>
</html>
-
π€ Where is it used?
- πΊοΈ Google Maps & Navigation Apps
- π Food delivery & Ride-sharing apps
- π€οΈ Weather apps that show location-based updates
- π Security & Tracking systems
-
π‘οΈ Privacy Considerations:
- π Always ask for user permission before using location
- π€π» Do not store or share location data without consent
π‘ Tip: Always handle user consent and fallback options when using a Geolocation API to ensure a seamless experience!πα―β€
I've created Technical Presentation on this topic. You can refer to it, along with the slide notes for a better understanding.
ABOUT ME: π§π½ I'm Frontend Developer based in Stockholm, Sweden, currently working at SITA.dev. My transition into tech has been a unique and rewarding adventure, fueled by curiosity and determination. You can connect with me on LinkedIn
Top comments (1)
Thank you :-)