HTML5 जियोलोकेशन उपयोगकर्ता का स्थान पता लगाने के लिए उपयोग किया जाता है। यह बिना SSL कनेक्शन के काम कर सकता है। इसे निम्नानुसार उत्पन्न किया जा सकता है:
// Variable apigeo shows successful location:
var apigeo = function(position) {
alert("API geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};
var tryapigeo = function() {
jQuery.post(
"check the location on google map",
function(success) {
apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}});
}) // Gives success on given geolocation
.fail(function(err) {
// On failure, alert with failure is shown
alert("error while showing geolocation! \n\n" + err);
});
};
फिर विभिन्न एरर कोड्स के लिए स्विच केस उत्पन्न किए जा सकते हैं, अलग-अलग त्रुटियों के लिए अलग एरर कोड।
var tryloc = function() { // tryloc variable shows current position
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
browserGeolocationSuccess, browserGeolocationFail, {
maximumAge: 70000, timeout: 40000, enableHighAccuracy: true
});
// this will show result with high accuracy
}
};
महत्वपूर्ण: आधुनिक ब्राउज़र्स HTTPS (SSL) आवश्यक करते हैं navigator.geolocation के लिए। HTTP पर यह काम नहीं करेगा (सुरक्षा नीति)। लोकलहोस्ट अपवाद है। enableHighAccuracy: true GPS उपयोग करता है (धीमा, अधिक बैटरी)। maximumAge कैश्ड पोजीशन की अधिकतम आयु। timeout मिलीसेकंड में।