標籤:type 執行 long charset meta 設定 ntp 開發人員 console
html5提供的地理位置定位使開發人員不用藉助其他軟體就能輕鬆實現位置尋找,地圖應用,導航等功能。
地理位置定位基本原理
GPS, WIFI, IP, 手機訊號基站
核心對象
Geolocation是window.navigator下面的一個對象,該對象提供了實現地理位置定位的介面。
要用該功能需先判斷瀏覽器是否支援navigator.geolocation對象。
navigator.geolocation.getCurrentPosition(success, error, options);
success(position) 擷取資訊成功的回呼函數
對象:
coords.latitude(十進位數的緯度)coords.longitude(十進位數的經度)coords.altitude(海拔,海平面以上以米計)coords.accuracy(位置精確度)coords.altitudeAccuracy(位置的海拔精度)coords.heading(方向,從正北開始以度計)coords.speed(速度,以米/每秒計)timestamp(響應的日期/時間)
error(errorcode) 擷取資訊失敗的回呼函數
code是錯誤碼message是錯誤資訊
options設定配置參數,對象
enableHighAccuracy 表示是否允許使用高精度timeout指定逾時時間maximumAge是指緩衝的時間
geolocation還有兩個方法:
1、watchPosition(success, error, options)表示重複擷取地理位置,相當於不斷執行getCurrentPosition。
2、clearWatch()用來清除前一次擷取的位置資訊。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body></body><script> if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { console.log("緯度:" + position.coords.latitude); console.log("經度:" + position.coords.longitude); }, function(error) { console.log(error.message); }, {}); }</script></html>
html5的地理位置定位