Geolocation API in HTML

Geolocation API in HTML ka use user ki current location lene ke liye hota hai. Geolocation API in HTML web applications ko latitude aur longitude jaise location data access karne ki facility deta hai.


User ki Position kaise Locate karte hain

Geolocation API in HTML user ki current location access karta hai. Kyuki yeh privacy ko affect kar sakta hai, isliye jab tak user permission approve nahi karta tab tak location available nahi hoti.

Note: Geolocation API in HTML sirf secure context me kaam karta hai, jaise HTTPS.

Tip: Yeh API GPS wale devices par sabse zyada accurate hota hai, jaise smartphone ya smartwatch.


HTML Geolocation API ka Use Kaise Karein

Geolocation API in HTML ko access karne ke liye navigator.geolocation ka use kiya jata hai.

Jab aap isse call karte ho, browser user se permission mangta hai. Agar user allow karta hai, to browser device me best available system (jaise GPS) use karke location nikalta hai.

getCurrentPosition() method user ki current location return karta hai.

Geolocation API in HTML

Example ka Explanation

  • Pehle check kiya jata hai ki browser Geolocation support karta hai ya nahi

  • Agar support karta hai to getCurrentPosition() method run hota hai

  • showPosition() function latitude aur longitude display karta hai

  • showError() function tab run hota hai jab koi error aata hai


Error Handling aur Rejections

getCurrentPosition() ka second parameter error handle karne ke liye hota hai. Agar location nahi milti, to yeh function run hota hai.

Example:

Error Handling aur Rejections


Location-Specific Information

Geolocation API in HTML ka use location-based information ke liye bhi hota hai, jaise:

  • Local area ki updated information dikhana

  • User ke paas ke points-of-interest dikhana

  • Turn-by-turn navigation (GPS system jaisa)


getCurrentPosition() Method kya Return karta hai

Jab yeh method successfully run hota hai, to yeh ek object return karta hai.

Yeh properties hamesha milti hain:

  • coords.latitude – Latitude decimal number me

  • coords.longitude – Longitude decimal number me

  • coords.accuracy – Position ki accuracy

Agar available ho to yeh bhi mil sakti hain:

  • coords.altitude – Sea level se height (meters me)

  • coords.altitudeAccuracy – Height ki accuracy

  • coords.heading – North se degree me direction

  • coords.speed – Speed meters per second me

  • timestamp – Date aur time jab response mila


Geolocation Object ke Other Methods

watchPosition()

watchPosition() method user ki location return karta hai aur user move kare to updated location bhi deta rehta hai. Yeh car ke GPS system jaisa kaam karta hai. Yeh bhi Geolocation API in HTML ka important feature hai.

clearWatch()

clearWatch() method watchPosition() ko stop karne ke liye use hota hai.

watchPosition() ka Example

watchPosition()


Is tarah Geolocation API in HTML developers ko user ki location access karne ki facility deta hai, jisse location-based services jaise maps, navigation aur local information web applications me easily implement ki ja sakti hain.

Leave a Comment