這篇文章給大家分享的是有關(guān)微信小程序如何獲取地理位置及經(jīng)緯度授權(quán)的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
這篇文章主要介紹了微信小程序獲取地理位置及經(jīng)緯度授權(quán)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
微信小程序獲取地理位置授權(quán),首先需要在app.json中添加配置:
"permission": { "scope.userLocation": { "desc": "請(qǐng)確認(rèn)授權(quán)" } }
獲取經(jīng)緯度:如果手機(jī)未開(kāi)啟位置信息,那么授權(quán)成功后在wx.getLocation()方法中也會(huì)一直失敗,所以需要在fail方法中提示用戶開(kāi)啟手機(jī)位置信息
getUserLocation: function () { let vm = this wx.getSetting({ success: (res) => { // res.authSetting['scope.userLocation'] == undefined 表示 初始化進(jìn)入該頁(yè)面 // res.authSetting['scope.userLocation'] == false 表示 非初始化進(jìn)入該頁(yè)面,且未授權(quán) // res.authSetting['scope.userLocation'] == true 表示 地理位置授權(quán) // 拒絕授權(quán)后再次進(jìn)入重新授權(quán) if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) { // console.log('authSetting:status:拒絕授權(quán)后再次進(jìn)入重新授權(quán)', res.authSetting['scope.userLocation']) wx.showModal({ title: '', content: '【泰福利Lite】需要獲取你的地理位置,請(qǐng)確認(rèn)授權(quán)', success: function (res) { if (res.cancel) { wx.showToast({ title: '拒絕授權(quán)', icon: 'none' }) setTimeout(() => { wx.navigateBack() }, 1500) } else if (res.confirm) { wx.openSetting({ success: function (dataAu) { // console.log('dataAu:success', dataAu) if (dataAu.authSetting["scope.userLocation"] == true) { //再次授權(quán),調(diào)用wx.getLocation的API vm.getLocation(dataAu) } else { wx.showToast({ title: '授權(quán)失敗', icon: 'none' }) setTimeout(() => { wx.navigateBack() }, 1500) } } }) } } }) } // 初始化進(jìn)入,未授權(quán) else if (res.authSetting['scope.userLocation'] == undefined) { // console.log('authSetting:status:初始化進(jìn)入,未授權(quán)', res.authSetting['scope.userLocation']) //調(diào)用wx.getLocation的API vm.getLocation(res) } // 已授權(quán) else if (res.authSetting['scope.userLocation']) { // console.log('authSetting:status:已授權(quán)', res.authSetting['scope.userLocation']) //調(diào)用wx.getLocation的API vm.getLocation(res) } } }) }, // 微信獲得經(jīng)緯度 getLocation: function (userLocation) { let vm = this wx.getLocation({ type: "wgs84", success: function (res) { // console.log('getLocation:success', res) var latitude = res.latitude var longitude = res.longitude vm.getDaiShu(latitude, longitude) }, fail: function (res) { // console.log('getLocation:fail', res) if (res.errMsg === 'getLocation:fail:auth denied') { wx.showToast({ title: '拒絕授權(quán)', icon: 'none' }) setTimeout(() => { wx.navigateBack() }, 1500) return } if (!userLocation || !userLocation.authSetting['scope.userLocation']) { vm.getUserLocation() } else if (userLocation.authSetting['scope.userLocation']) { wx.showModal({ title: '', content: '請(qǐng)?jiān)谙到y(tǒng)設(shè)置中打開(kāi)定位服務(wù)', showCancel: false, success: result => { if (result.confirm) { wx.navigateBack() } } }) } else { wx.showToast({ title: '授權(quán)失敗', icon: 'none' }) setTimeout(() => { wx.navigateBack() }, 1500) } } }) }
感謝各位的閱讀!關(guān)于“微信小程序如何獲取地理位置及經(jīng)緯度授權(quán)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
文章標(biāo)題:微信小程序如何獲取地理位置及經(jīng)緯度授權(quán)-創(chuàng)新互聯(lián)
文章地址:http://chinadenli.net/article16/dojjgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、手機(jī)網(wǎng)站建設(shè)、域名注冊(cè)、動(dòng)態(tài)網(wǎng)站、網(wǎng)站營(yíng)銷、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容