我正在使用HTML 5 GEO位置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>
当我在CODIN Chrome
和FireFox
上运行时,它给了我准确的值122000
,而在IE
中,它给出了15个(更合适)因此,我要做的事情以获得更准确的结果
W3C地理位置API当前在主要浏览器中支持。
- Internet Explorer(9.0 )
- Safari(5.0 )
- Firefox(3.5 )
- 歌剧(10.6 )
- Google Chrome(取决于OS)
显然您应该使用navigator.geolocation.getCurrentPosition
在任何受支持的浏览器中完成此操作。
它的准确程度取决于浏览器供应商。您可能会从每个结果中获得略有不同的结果。
另一种选择是Maxmind的Geolite服务,这是一项免费服务,他们每个月的第一个星期二每个星期二更新数据库。您可以检索此二进制或简历格式。
对于移动浏览器,您可以在GPS网站上查看,但是大多数现代移动浏览器也可以支持HTML5地理位置。