HTML 5 GeoLocation准确性

时间:2014-01-28 09:30:32

标签: html5 geolocation

我正在使用HTML 5 Geo Location Api 并使用此代码

<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<script>
    var x=document.getElementById("demo");
    function getLocation()
    {
        if (navigator.geolocation)
        {
            navigator.geolocation.watchPosition(showPosition,error,{ enableHighAccuracy: true, timeout: 150000, maximumAge: 0 });
        }
        else
        {
            x.innerHTML="Geolocation is not supported by this browser.";
        }
    }
    function showPosition(position)
    {
        x.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +
            'Longitude: ' + position.coords.longitude     + '<br />' +'Acc: ' + position.coords.accuracy     + '<br />'+
            '<hr />'      + x.innerHTML;
    }
    function error(err){alert(err);}
</script>

当我在代码ChromeFireFox上面运行时,它给出了我的准确度值122000,而在IE中它给出了15(这更合适) 那么我该做些什么来获得更准确的结果

1 个答案:

答案 0 :(得分:0)

主要浏览器目前支持

W3C Geolocation API

  • Internet Explorer(9.0 +)
  • Safari(5.0 +)
  • Firefox(3.5 +)
  • Opera(10.6 +)
  • 谷歌浏览器(取决于操作系统)

显然,您应该使用navigator.geolocation.getCurrentPosition在任何支持的浏览器中完成此操作。

它的准确程度取决于浏览器供应商。你可能会得到每个结果略有不同的结果。

另一个替代方案是来自MaxMind的GeoLite service,它是一项免费服务,他们每个月的第一个星期二更新他们的数据库。您可以检索二进制或cvs格式。

对于移动浏览器,您可以查看GPS Web Gate,但大多数现代移动浏览器也可以支持HTML5 GeoLocation。

相关问题