Skip to content Skip to sidebar Skip to footer

What Is The Best Jquery Plugin That Handles Html5 Location?

I want to get the lat/long for a browser. I know that HTML5 has the feature, but I want a full plugin that handles old browsers gracefully. Is there a reliable plugin for this?

Solution 1:

Do you really need a plugin for something so simple ?

if (navigator.geolocation) {
   navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}else{
   errorFunction();
}

functionsuccessFunction(position) { //uses HTML5 if availablevar lat = position.coords.latitude;
    var lng = position.coords.longitude;
}

functionerrorFunction(){ //uses IP if no HTML5
   $.getJSON("http://freegeoip.net/json/", function(res){
        var lat = res.latitude;
        var lng = res.longitude;
   });
}

FIDDLE

This checks if the HTML5 geolocation API is supported, if it is, it gets the coordinates, if it's not, or if the HTML5 geo API for some reason fails, it uses a free geoIP service to get the coordinates.

Post a Comment for "What Is The Best Jquery Plugin That Handles Html5 Location?"