为什么选择 HTML5 地理位置?

时间:2023-04-20
本文介绍了为什么选择 HTML5 地理位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

为什么 HTML5 地理位置可以让您共享您的位置?使用地理位置的主要目的是什么,因为您也可以通过 IP 地址获取位置.这两种方法有什么区别吗?

Why does HTML5 geolocation let you share your location? What is main purpose of using geolocation, as you can get the location with IP address as well. Is there any difference between these two methods?

我之所以问,是因为地理定位需要用户的许可,并且也不适用于所有浏览器.

I'm asking because geolocation requires the user's permission and also doesn't work on all browsers.

推荐答案

HTML5 GeoLocation 往往比基于 IP 的 GeoLocation 更准确很多.

HTML5 GeoLocation tends to be much more accurate than IP-based GeoLocation.

基于 IP 的 GeoLocation 依赖于与 ISP 关联的数据库来确定您的位置.当您的 ISP 服务于非常大的区域并提供动态 IP 地址时,这将无法正常工作.今天在一个城镇的地址可能在明天 100 英里之外.此外,这些数据库通常不会经常更新.如果您的 ISP 出售 IP 块或将它们转移到新城镇,数据库可能仍会错误地认为您在其他地方.

IP-based GeoLocation depends on databases associated with ISPs to figure out where you are. This doesn't work well when your ISP services a very large area and gives out dynamic IP addresses. The address in one town today might be 100 miles away tomorrow. Furthermore, those databases are usually not updated frequently. If your ISP sells off blocks of IPs or moves them to a new town, the database may still incorrectly think you're somewhere else.

HTML5 位置使用浏览器提供的服务来确定您的位置.如果您的计算机内置 GPS(例如在许多移动设备和某些笔记本电脑上),它会准确地知道您在哪里.这使得它对于具有导航或位置组件的 web 应用程序更加有用.对于没有 GPS 的设备,它通常可以根据附近已知的无线信号和其他因素提供非常好的近似值,例如跟踪您的计算机在连接到互联网时经过的路由器.确切的实现取决于计算机、可用的硬件以及浏览器选择执行操作的方式.

HTML5 location uses services provided by your browser to figure out where you are. If your computer has GPS built-in (such as on many mobile devices and some laptops), it will know exactly where you are. This makes it much more useful for webapps that have a navigation or location component. For devices without GPS, it can often provide a very good approximation based on nearby known wireless signals and other factors, such as tracing what routers your computer goes through when connecting to the internet. The exact implementation depends on the computer, what hardware it has available, and how the browser chooses to do things.

例如,当我查看基于 IP 的定位服务时,它显示我在我所居住的同一地区的一个特定大城市,但它实际上大约在 50 英里之外.当我使用基于 HTML5 位置的服务来确定我在哪里时,它只相差大约 20 个blocks.

For example, when I check an IP-based location service, it says that I'm in a particular large city in the same general area that I live in, but it's actually about 50 miles away. When I use an HTML5 location based service to figure out where I am, it's only off by about 20 blocks.

如果您正在开发需要位置数据的网络应用程序,尝试尽可能使用 HTML5 GeoLocation.设置回退,以便 如果 HTML5 定位失败,您可以改用 GeoIP 解决方案.这样,您将在受支持的浏览器和设备上获得最佳准确性,但在 HTML5 数据不可用时仍然有适当的解决方案.

If you're developing a webapp which needs location data, try using HTML5 GeoLocation if at all possible. Set up a fallback, so that if HTML5 location fails, you can use an GeoIP solution instead. This way, you will get optimal accuracy on supported browsers and devices, but will still have a solution in place when the HTML5 data are not available.

如果您使用了来自 Google Code 的 geolocation-javascript 项目,您可以使用以下代码:

If you used the geolocation-javascript project from Google Code, you could use the following code:

//determine if device has geo location capabilities
if(geo_position_js.init()){
   geo_position_js.getCurrentPosition(success_callback,error_callback);
}
else{
   // use an AJAX request to look up the location based on IP address
}

这篇关于为什么选择 HTML5 地理位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:地理位置:仅移动谷歌地图标记而不重新加载地图 下一篇:根据 lat & 获取邮政编码长的?

相关文章