HTML5 जियोलोकेशन API आपको अपनी लोकेशन अपनी पसंदीदा वेबसाइटों के साथ साझा करने देता है। एक JavaScript आपकी अक्षांश (latitude) और देशांतर (longitude) प्राप्त कर सकता है, जिसे बैकएंड वेब सर्वर पर भेजा जा सकता है, और फिर स्थानीय व्यवसायों को ढूंढने या मैप पर अपनी लोकेशन दिखाने जैसे लोकेशन-अवेयर कार्य किए जा सकते हैं।
उदाहरण
आइए देखें कि वर्तमान स्थिति कैसे प्राप्त करें:
<!DOCTYPE HTML> <html> <head> <script> function showLocation(position) { var latitude = position.coords.latitude; var longitude = position.coords.longitude; alert("Latitude : " + latitude + " Longitude: " + longitude); } function errorHandler(err) { if(err.code == 1) { alert("Error: Access is denied!"); } else if( err.code == 2) { alert("Error: Position is unavailable!"); } } function getLocation(){ if(navigator.geolocation){ // 60000 मिलीसेकंड (60 सेकंड) पर टाइमआउट var options = {timeout:60000}; navigator.geolocation.getCurrentPosition(showLocation, errorHandler, options); } else{ alert("Sorry, browser does not support geolocation!"); } } </script> </head> <body> <form> <input type="button" onclick="getLocation();" value="Get Location"/> </form> </body> </html>