为什么选择HTML5地理定位? [英] Why HTML5 Geolocation?

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

问题描述

为什么HTML5地理位置让你分享你的位置?使用地理定位的主要目的是什么,因为您可以使用IP地址获取位置。这两种方法有什么不同吗?



我在问,因为地理位置需要用户的许可,并且不适用于所有浏览器。

解决方案

HTML5 GeoLocation往往比基于IP的GeoLocation更精确。

基于IP的地理位置取决于与ISP相关的数据库,以确定您的位置。当你的ISP服务于一个非常大的区域并发布动态IP地址时,这并不适用。今天在一个城镇的地址可能在100英里外。此外,这些数据库通常不会经常更新。如果您的ISP销售IP块或将其移到新城区,数据库可能仍然错误地认为您在其他地方。



HTML5位置使用您提供的服务浏览器找出你在哪里。如果您的计算机内置了GPS(例如在许多移动设备和一些便携式计算机上),它将完全知道您的位置。 这使得它对具有导航或位置组件的webapps更有用。对于没有GPS的设备,它通常可以根据附近已知的无线信号和其他因素提供非常好的近似值,例如跟踪连接到互联网时计算机所经过的路由器。确切的实现取决于计算机,它有哪些可用的硬件,以及浏览器如何选择做事情。

例如,当我检查基于IP的位置服务,它说我在一个特定的大城市,与我居住的同一个地区相同,但实际上它距离大约50英里。
当我使用HTML5基于位置的服务来确定我的位置时,它只能关闭大约20个

如果您正在开发需要位置数据的web应用程序,请尽可能使用HTML5 GeoLocation尝试。设置回退,以便如果 HTML5的位置失败,则可以使用GeoIP解决方案。通过这种方式,您将在支持的浏览器和设备上获得最佳的准确性,但在HTML5数据不可用时仍会有适当的解决方案。



如果您使用< http://code.google.com/p/geo-location-javascript/rel =noreferrer> Google代码中的geolocation-javascript项目,您可以使用以下代码:

  //确定设备是否具有地理位置功能
if(geo_position_js.init()){
geo_position_js。 getCurrentPosition(success_callback,error_callback);
}
else {
//使用AJAX请求根据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 tends to be much more accurate than IP-based GeoLocation.

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 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.

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.

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.

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地理定位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆