html5的地理位置定位

來源:互聯網
上載者:User

標籤: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的地理位置定位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.