HTML5 Geolocation
HTML5 Geolocation(地理定位)用于定位用戶的位置。
定位用戶的位置
HTML5 Geolocation API 用于獲得用戶的地理位置。
鑒于該特性可能侵犯用戶的隱私,除非用戶同意,否則用戶位置信息是不可用的。
瀏覽器支持
Internet Explorer 9+, Firefox, Chrome, Safari 和 Opera 支持Geolocation(地理定位).
注意: Geolocation(地理定位)對于擁有 GPS 的設(shè)備,比如 iPhone,地理定位更加精確。
HTML5 - 使用地理定位
請使用 getCurrentPosition() 方法來獲得用戶的位置。
下例是一個簡單的地理定位實例,可返回用戶位置的經(jīng)度和緯度:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p id="demo">點擊按鈕獲取您當(dāng)前坐標(biāo)(可能需要比較長的時間獲取):</p> <button onclick="getLocation()">點我</button> <script> var x=document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else{x.innerHTML="該瀏覽器不支持獲取地理位置。";} } function showPosition(position) { x.innerHTML="緯度: " + position.coords.latitude + "<br>經(jīng)度: " + position.coords.longitude; } </script> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
實例解析:
檢測是否支持地理定位
如果支持,則運行 getCurrentPosition() 方法。如果不支持,則向用戶顯示一段消息。
如果getCurrentPosition()運行成功,則向參數(shù)showPosition中規(guī)定的函數(shù)返回一個coordinates對象
showPosition() 函數(shù)獲得并顯示經(jīng)度和緯度
上面的例子是一個非?;A(chǔ)的地理定位腳本,不含錯誤處理。
處理錯誤和拒絕
getCurrentPosition() 方法的第二個參數(shù)用于處理錯誤。它規(guī)定當(dāng)獲取用戶位置失敗時運行的函數(shù):
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p id="demo">點擊按鈕獲取您當(dāng)前坐標(biāo)(可能需要比較長的時間獲?。?lt;/p> <button onclick="getLocation()">點我</button> <script> var x=document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition,showError); } else{x.innerHTML="Geolocation is not supported by this browser.";} } function showPosition(position) { x.innerHTML="緯度: " + position.coords.latitude + "<br>經(jīng)度: " + position.coords.longitude; } function showError(error) { switch(error.code) { case error.PERMISSION_DENIED: x.innerHTML="用戶拒絕對獲取地理位置的請求。" break; case error.POSITION_UNAVAILABLE: x.innerHTML="位置信息是不可用的。" break; case error.TIMEOUT: x.innerHTML="請求用戶地理位置超時。" break; case error.UNKNOWN_ERROR: x.innerHTML="未知錯誤。" break; } } </script> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
錯誤代碼:
Permission denied - 用戶不允許地理定位
Position unavailable - 無法獲取當(dāng)前位置
Timeout - 操作超時
在地圖中顯示結(jié)果
如需在地圖中顯示結(jié)果,您需要訪問可使用經(jīng)緯度的地圖服務(wù),比如谷歌地圖或百度地圖:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p id="demo">點擊按鈕獲取您當(dāng)前坐標(biāo)(可能需要比較長的時間獲?。?lt;/p> <button onclick="getLocation()">點我</button> <div id="mapholder"></div> <script> var x=document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition,showError); } else{x.innerHTML="該瀏覽器不支持獲取地理位置。";} } function showPosition(position) { var latlon=position.coords.latitude+","+position.coords.longitude; var img_url="http://maps.googleapis.com/maps/api/staticmap?center=" +latlon+"&zoom=14&size=400x300&sensor=false"; document.getElementById("mapholder").innerHTML="<img src='"+img_url+"'>"; } function showError(error) { switch(error.code) { case error.PERMISSION_DENIED: x.innerHTML="用戶拒絕對獲取地理位置的請求。" break; case error.POSITION_UNAVAILABLE: x.innerHTML="位置信息是不可用的。" break; case error.TIMEOUT: x.innerHTML="請求用戶地理位置超時。" break; case error.UNKNOWN_ERROR: x.innerHTML="未知錯誤。" break; } } </script> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
在上例中,我們使用返回的經(jīng)緯度數(shù)據(jù)在谷歌地圖中顯示位置(使用靜態(tài)圖像)。
Google地圖腳本
上面的鏈接向您演示如何使用腳本來顯示帶有標(biāo)記、縮放和拖曳選項的交互式地圖。
給定位置的信息
本頁演示的是如何在地圖上顯示用戶的位置。不過,地理定位對于給定位置的信息同樣很有用處。
實例:
更新本地信息
顯示用戶周圍的興趣點
交互式車載導(dǎo)航系統(tǒng) (GPS)
getCurrentPosition() 方法 - 返回數(shù)據(jù)
T若成功,則 getCurrentPosition() 方法返回對象。始終會返回 latitude、longitude 以及 accuracy 屬性。如果可用,則會返回其他下面的屬性。
屬性 | 描述 |
---|---|
coords.latitude | 十進制數(shù)的緯度 |
coords.longitude | 十進制數(shù)的經(jīng)度 |
coords.accuracy | 位置精度 |
coords.altitude | 海拔,海平面以上以米計 |
coords.altitudeAccuracy | 位置的海拔精度 |
coords.heading | 方向,從正北開始以度計 |
coords.speed | 速度,以米/每秒計 |
timestamp | 響應(yīng)的日期/時間 |
Geolocation 對象 - 其他有趣的方法
watchPosition() - 返回用戶的當(dāng)前位置,并繼續(xù)返回用戶移動時的更新位置(就像汽車上的 GPS)。
clearWatch() - 停止 watchPosition() 方法
下面的例子展示 watchPosition() 方法。您需要一臺精確的 GPS 設(shè)備來測試該例(比如 iPhone):
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p id="demo">點擊按鈕獲取您當(dāng)前坐標(biāo)(可能需要比較長的時間獲?。?lt;/p> <button onclick="getLocation()">點我</button> <script> var x=document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.watchPosition(showPosition); } else{x.innerHTML="該瀏覽器不支持獲取地理位置。";} } function showPosition(position) { x.innerHTML="緯度: " + position.coords.latitude + "<br>經(jīng)度: " + position.coords.longitude; } </script> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例