問題描述
為什么 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 的設備,它通??梢愿鶕浇阎臒o線信號和其他因素提供非常好的近似值,例如跟蹤您的計算機在連接??到互聯網時經過的路由器.確切的實現取決于計算機、可用的硬件以及瀏覽器選擇執行操作的方式.
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 地理位置?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!