這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)如何在HTML5中使用Geolocation API,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、成都小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了太子河免費建站歡迎大家使用!
Geolocation是HTML5標準下的一個Web API,利用它可以獲取設(shè)備的當前位置信息(坐標),此API具有三個方法:getCurrentPosition、watchPosition和clearWatch,其中最常用的是getCurrentPosition方法,剩下兩個方法需要搭配使用!
使用方法:
瀏覽器兼容性檢測:
該api通過navigator.geolocation對象發(fā)布,只有在此對象存在的情況下,才可以使用它的地理定位服務,檢測方法如下:
if (navigator.geolocation) { // 定位代碼寫在這里 } else { alert('Geolocation is not supported in your browser') }
獲取用戶的當前位置:
使用getCurrentLocation方法即可獲取用戶的位置信息,該方法有三個參數(shù):
參數(shù)列表 | 類型 | 說明 |
handleSuccess | Function | 成功時調(diào)用函數(shù)handleSuccess |
handleError | Function | 失敗時調(diào)用函數(shù)handleError |
options | Object | 初始化參數(shù) |
// 初始化參數(shù) const options = { // 高精確度: true / false enableHighAccuracy: true, // 等待響應的最長時間 單位:毫秒 timeout: 5 * 1000, // 應用程序愿意接受的緩存位置的最長時間 maximumAge: 0 } // 成功回調(diào)函數(shù) : data包含位置信息 const handleSuccess = data => console.log(data) // 失敗回調(diào)函數(shù) : error包含錯誤信息 const handleError = error => console.log(error) if (navigator.geolocation) { // 定位代碼寫在這里 navigator.geolocation.getCurrentPosition(handleSuccess, handleError, options) } else { alert('Geolocation is not supported in your browser') }
下面是具有更多細節(jié)的代碼:
const handleSuccess = data => { const { coords, // 位置信息 timestamp // 成功獲取位置信息時的時間戳 } = data const { accuracy, // 返回結(jié)果的精度(米) altitude, // 相對于水平面的高度 altitudeAccuracy, // 返回高度的精度(米) heading, // 主機設(shè)備的行進方向,從正北方向順時針方向 latitude, // 緯度 longitude, // 經(jīng)度 speed // 設(shè)備的行進速度 } = coords // 打印出來看看 console.log('timestamp =', timestamp) console.log('accuracy =', accuracy) console.log('altitude =', altitude) console.log('altitudeAccuracy =', altitudeAccuracy) console.log('heading =', heading) console.log('latitude =', latitude) console.log('longitude =', longitude) console.log('speed =', speed) } const handleError = error => { switch (error.code) { case 1: console.log('位置服務請求被拒絕') break case 2: console.log('暫時獲取不到位置信息') break case 3: console.log('獲取信息超時') break case 4: console.log('未知錯誤') break } } const opt = { // 高精確度: true / false enableHighAccuracy: true, // 等待響應的最長時間 單位:毫秒 timeout: 5 * 1000, // 應用程序愿意接受的緩存位置的最大年限 maximumAge: 0 } if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(handleSuccess, handleError, opt) } else { alert('Geolocation is not supported in your browser') }
上述就是小編為大家分享的如何在HTML5中使用Geolocation API了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:如何在HTML5中使用GeolocationAPI
網(wǎng)站地址:http://www.ekvhdxd.cn/article16/pdpodg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、關(guān)鍵詞優(yōu)化、定制開發(fā)、虛擬主機、品牌網(wǎng)站建設(shè)、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)