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 …


This content originally appeared on DEV Community and was authored by Himanay Khajuria

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)  
}

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;  
     }  
  • 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?

    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


This content originally appeared on DEV Community and was authored by Himanay Khajuria


Print Share Comment Cite Upload Translate Updates
APA

Himanay Khajuria | Sciencx (2025-02-23T16:50:23+00:00) Geolocation API เชœโ€ ๐Ÿ“๐Ÿ ๏ธŽ. Retrieved from https://www.scien.cx/2025/02/23/geolocation-api-%e0%aa%9c%e2%81%80-%f0%9f%93%8d%f0%9f%8f%a0%ef%b8%8e/

MLA
" » Geolocation API เชœโ€ ๐Ÿ“๐Ÿ ๏ธŽ." Himanay Khajuria | Sciencx - Sunday February 23, 2025, https://www.scien.cx/2025/02/23/geolocation-api-%e0%aa%9c%e2%81%80-%f0%9f%93%8d%f0%9f%8f%a0%ef%b8%8e/
HARVARD
Himanay Khajuria | Sciencx Sunday February 23, 2025 » Geolocation API เชœโ€ ๐Ÿ“๐Ÿ ๏ธŽ., viewed ,<https://www.scien.cx/2025/02/23/geolocation-api-%e0%aa%9c%e2%81%80-%f0%9f%93%8d%f0%9f%8f%a0%ef%b8%8e/>
VANCOUVER
Himanay Khajuria | Sciencx - » Geolocation API เชœโ€ ๐Ÿ“๐Ÿ ๏ธŽ. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/23/geolocation-api-%e0%aa%9c%e2%81%80-%f0%9f%93%8d%f0%9f%8f%a0%ef%b8%8e/
CHICAGO
" » Geolocation API เชœโ€ ๐Ÿ“๐Ÿ ๏ธŽ." Himanay Khajuria | Sciencx - Accessed . https://www.scien.cx/2025/02/23/geolocation-api-%e0%aa%9c%e2%81%80-%f0%9f%93%8d%f0%9f%8f%a0%ef%b8%8e/
IEEE
" » Geolocation API เชœโ€ ๐Ÿ“๐Ÿ ๏ธŽ." Himanay Khajuria | Sciencx [Online]. Available: https://www.scien.cx/2025/02/23/geolocation-api-%e0%aa%9c%e2%81%80-%f0%9f%93%8d%f0%9f%8f%a0%ef%b8%8e/. [Accessed: ]
rf:citation
» Geolocation API เชœโ€ ๐Ÿ“๐Ÿ ๏ธŽ | Himanay Khajuria | Sciencx | https://www.scien.cx/2025/02/23/geolocation-api-%e0%aa%9c%e2%81%80-%f0%9f%93%8d%f0%9f%8f%a0%ef%b8%8e/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.