DEV Community

Cover image for Geolocation API ΰͺœβ€ πŸ“πŸ οΈŽ
Himanay Khajuria
Himanay Khajuria

Posted on • Edited on

Geolocation API ΰͺœβ€ πŸ“πŸ οΈŽ

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)
}
Enter fullscreen mode Exit fullscreen mode

SYNTAX EXPLANATION


πŸ‘‰ 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β“πŸ€”πŸ’­

    1. Uses GPS, Wi-Fi & IP address to get the location
    2. Always returns latitude, longitude and accuracy properties
    3. Other properties are returned if available
    4. 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;  
     }  
Enter fullscreen mode Exit fullscreen mode
  • 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;  
    }  
        }  
Enter fullscreen mode Exit fullscreen mode

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>  
Enter fullscreen mode Exit fullscreen mode

  • πŸ€” Where is it used?

    1. πŸ—ΊοΈ Google Maps & Navigation Apps
    2. πŸ• Food delivery & Ride-sharing apps
    3. 🌀️ Weather apps that show location-based updates
    4. πŸ”’ 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)

Collapse
 
artydev profile image
artydev

Thank you :-)