Geolocation API用於將用戶當前地理位置信息共享給信任的站點,這涉及用戶的隱私安全問題,所以當一個站點需要獲取用戶的當前地理位置,瀏覽器會提示用戶是“允許” or “拒絕”。
先看看哪些瀏覽器支持Geolocation API:
IE9.0+、FF3.5+、Safari5.0+、Chrome5.0+、Opera10.6+、IPhone3.0+、Android2.0+
也就是說除IE6~IE8外,其它最新的瀏覽器基本上都支持,包括最新的移動手機。
Geolocation API存在於navigator對象中,只包含3個方法:
1、getCurrentPosition
2、watchPosition
3、clearWatch
getCurrentPosition、watchPosition的參數說明,示例:
navigator.geolocation.getCurrentPosition(success_callback, error_callback, {geolocation選項});
第一個參數是用戶允許瀏覽器共享geolocation成功後的回調方法
第二個參數是用獲取地理位置信息失敗的處理方法,傳入錯誤對象,包含code、message兩個屬性
第三個參數都是geolocation選項,所有的geolocation選項都是可選的,它包含的屬性如下:
enableHighAccuracy(Boolean型,默認爲false,是否嘗試更精確地讀取緯度和經度,移動設備上,這可能要使用手機上的GPS,這會消耗移動設備更多的電量)
timeout(單位爲毫秒,默認值爲0,在放棄並觸發處理程序之前,可以等待的時間----用戶選擇期間是不計時的)
maximumAge(單位爲毫秒,默認值爲0。用來告訴瀏覽器是否使用最近緩存的位置數據,如果在maximumAge內有一個請求,將會返回它,而不請求新位置。maximumAge如果爲Infinity,則總是使用一個緩存的位置,如果爲0則必須在每次請求時查找一個新位置)
簡單的一個示例:
當我點擊拒絕時:
當點擊允許時:
html源代碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>HTML5 Geolocation</title> <style> body {background-color:#fff;} </style> </head> <body> <p id="geo_loc"><p> <script> 1: 2: function getElem(id) { 3: return typeof id === 'string' ? document.getElementById(id) : id; 4: } 5: 6: function show_it(lat, lon) { 7: var str = '您當前的位置,緯度:' + lat + ',經度:' + lon; 8: getElem('geo_loc').innerHTML = str; 9: } 10: 11: if (navigator.geolocation) { 12: navigator.geolocation.getCurrentPosition(function(position) { 13: show_it(position.coords.latitude, position.coords.longitude); 14: }, function(err) { 15: getElem('geo_loc').innerHTML = err.code + "\n" + err.message; 16: }); 17: } else { 18: getElem('geo_loc').innerHTML = "您當前使用的瀏覽器不支持Geolocation服務"; 19: }</script> </body> </html>
上面的例子中,只使用了success_callback中的緯度(latitude)和經度(longitude),成功後回調獲取用戶位置數據position,它包含兩個屬性:coords、timestamp。
coords屬性有7個值,包含上面用到的緯度、經度。
1、accuracy 準確角
2、altitude 海拔高度
3、altitudeAcuracy 海拔高度的精確度
4、heading 行進方向
5、speed 地面的速度
根據獲得的緯度與經度,很容易將用戶的位置在google地圖中顯示出來,如下例所示:
核心的javascript腳本:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script> function getElem(id) { return typeof id === 'string' ? document.getElementById(id) : id; } function success(position) { var mapcanvas = document.createElement('div'); mapcanvas.id = 'mapcanvas'; mapcanvas.style.height = '400px'; mapcanvas.style.width = '560px'; getElem("map_canvas").appendChild(mapcanvas); var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); var myOptions = { zoom: 15, center: latlng, mapTypeControl: false, navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL}, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("mapcanvas"), myOptions); var marker = new google.maps.Marker({ position: latlng, map: map, title:"你在這裏!" }); } if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(success); } else { alert("您當前使用的瀏覽器不支持geolocation服務"); } </script>
watchPosition像一個追蹤器,與clearWatch成對。watchPosition與clearWatch有點像setInterval和clearInterval的工作方式。
var watchPositionId = navigator.geolocation.watchPosition(success_callback, error_callback, options);
navigator.geolocation.clearWatch(watchPositionId );
關於Geolocation API的更多參考:
W3C geolocation API
Gears
BlackBerry geolocation API
Nokia geolocation API
Palm geolocation API
OMTP BONDI geolocation API
geo.js
Internet Explorer 9 Guide for Developers: Geolocation
MDN Using geolocation
http://diveintohtml5.org/geolocation.html