【发布时间】:2014-10-01 19:28:33
【问题描述】:
我正在尝试做一个简单的地理位置 html5:
$(function() {
var currentLatitude ="";
var currentLongitude ="";
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
function showPosition(position) {
currentLatitude = position.coords.latitude;
currentLongitude = position.coords.longitude;
}
} else {
// location based on the IP - less accurate
}
即使我的网站(在本地运行)出现异常,错误也是位置错误的许可。 我的第二个选择是通过 IP (ipinfo.io):
function showError(error) {
var x = $('#curr_loc_target');
switch(error.code) {
case error.PERMISSION_DENIED:
x.html("User denied the request for Geolocation.");
jQuery.get("http://ipinfo.io/json", function (response)
{
console.log(response) ;
currentLatitude = response.loc.split(',')[0];
currentLongitude = response.loc.split(',')[1];
console.log("lat" + currentLatitude+ "," + "lngs" +currentLongitude) ;
} );
break;
case error.POSITION_UNAVAILABLE:
x.html("Location information is unavailable.");
break;
case error.TIMEOUT:
x.html("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
x.html("An unknown error occurred.");
break;
}
}
它有效,但它根本不准确。 我需要做的就是将 lat、lng 发送到我的服务器,如果它不是本地的,它会工作吗? 或者有人知道解决此错误的方法吗?
【问题讨论】:
-
我真的不知道 ipinfo 有什么样的准确性,但很明显基于 IP 的地理定位有几个缺点:它不适用于本地 IP,如果用户在代理后面,它是会返回一个错误的位置,它并不总是很准确。我编辑了一项服务:userinfo.io,它为您提供来自 IP 的地理位置(和其他信息)。到目前为止,我的测试表明它非常准确,但它具有我提到的相同缺点。我依赖于几个 geoip 提供商,所以也许它会更准确一些,但我不能确定。
标签: javascript ajax html geolocation