What is the best JQuery plugin that handles HTML5 location? [closed]

旧巷老猫 提交于 2019-12-12 03:43:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!