Forum

Yampiz Forum

Sharing let knowledge Spreaded, Reading let spirits growth, Creative let culture extended, Let us
  • 禁止廣告文
  • 知識傳播
  • 閱讀
  • 心靈成長
  • 創作
  • 文化傳承
New Subject
Random
Previous
Next
|
Web 網頁地理定位

Web 網頁地理定位 (Geo-Location)

線上地圖已經是很平常的應用,Google Map 甚至可以用作駕駛導航儀,然而,這些應用的實現涉及到千絲萬縷的艱辛工作,僅僅是定位你現在的位置,就有很多技術,有幸的是,如今在你的 Web 站點中,你可以通過短短的幾行代碼,獲得一個用戶的地理位置,本文介紹的是用在 Web 中的各種地理定位技術。

通過 IP 地址獲得用戶的地理位置信息

網際網路不過是一個巨大的網絡,上面的每一台電腦對應一個 IP 地址,通過這個 IP 地址,可以知道這個用戶在什麼位置,我們可以通過查詢 Maxmind GeoIP 數據庫獲得,Maxmind 還提供了一個 JavaScript 連接讓你在網站上實現這個功能。
<script type="text/javascript" src="http://j.maxmind.com/app/geoip.js"></script>
<script>
var info = document.getElementById('info');
var lat = geoip_latitude();
var lon = geoip_longitude();
var city = geoip_city();
var out = '<h3>Information from your IP</h3>'+
'<ul>'+
'<li>Latitude: ' + lat + '</li>'+
'<li>Longitude: ' + lon + '</li>'+
'<li>City: ' + city + '</li>'+
'<li>Region: ' + geoip_region() + '</li>'+
'<li>Region Name: ' + geoip_region_name() + '</li>'+
'<li>Postal Code: ' + geoip_postal_code() + '</li>'+
'<li>Country Code: ' + geoip_country_code() + '</li>'+
'<li>Country Name: ' + geoip_country_name() + '</li>'+
'</ul>'
info.innerHTML = out;
</script>
 
 
這個方法(演示)有一個問題,就是說,你獲得的 IP 地理位置信息未必非常准確,因為這個 IP 是你的 ISP 分配給你的,你獲得的事實上是 ISP 的地理位置,另外,如果你使用 VPN,你實際的得到可能是十萬八千裏之外的地方了。
 
通過 W3C Geo API 獲得用戶地理位置

通過 IP 地址獲取用戶地理位置還有一個棘手的問題,就是安全和隱私問題,畢竟,並不是每個用戶都願意自己的地理位置信息被別人知道,鑒於此,人們開發了很多技術,Google Gears 擁有一個 Geo-Location 服務,Plazes 幫助你保存你的位置,而 Yahoo Fire Eagle 算得上這類技術中最順手的一個。然而這些技術共同的問題是,它們要求用戶要麼安裝一個外掛插件,要麼需要訪問特定的 Web 服務,這有些麻煩。

W3C 有一個 Geo-Location API 可以幫助瀏覽器制造商,在瀏覽器中整合地理信息功能。Firefox 3.5 以上的瀏覽器支持 W3C Geo-LocationAPI,同樣支持該 API 的還有 iPhone 上的 Safari 瀏覽器(需要 OS3.0 以上)。需要通過瀏覽器查詢自己地理位置的用戶,在瀏覽器中,會顯示一個對話條,請求你的授權。
 
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(
function(position){
var lon = position.coords.longitude;
var lat = position.coords.latitude;
},
function(error){alert('ouch');}
);
}
  • Keywords : 用戶, script, 通過, 地理位置, 問題, 位置, position, Location, 獲得, 瀏覽器, 服務, 應用, 多技術, 地址, 以上, navigator, geolocation, function, coords, Region
0 0
2010-07-08T09:54:00+0000


  • Now, you can post comments by Facebook Account when your Yampiz account was logout or unvariable. whatever, we suggest to post comment by Yampiz Account to get more bounds to join new events of Heyxu
Comment
 
Verify
  • Auto Platform
    For better browsing experience, it will auto detects your device to display webpage.
  • Bug Report
    Tell us about the idea, problem and errors you found.
  • Comodo Secure
    The sensitive data transmission adopts by SSL-2048 authenticated encryption.
  • Copyright
    © 2009 YamPiz Digital Inc. & Jaby Group. All rights reserved.
  • Revised Version
    V2.2.061
    03 Aug 2023 (GMT+8)