问题
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?
回答1:
Do you really need a plugin for something so simple ?
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}else{
errorFunction();
}
function successFunction(position) { //uses HTML5 if available
var lat = position.coords.latitude;
var lng = position.coords.longitude;
}
function errorFunction(){ //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.
来源:https://stackoverflow.com/questions/15046508/what-is-the-best-jquery-plugin-that-handles-html5-location