uniapp微信小程序獲取當(dāng)前定位城市信息的實例代碼
一、事先準(zhǔn)備
此處用的是騰訊地圖的jdk
1、在騰訊地圖開發(fā)上申請key
2、 WebServiceAPI選擇簽名校驗獲取SK
3、 uniapp上勾選位置接口
4、在騰訊地圖上下載微信小程序javaScript SDK放入項目里
二、正式代碼使用
提示:可能會出現(xiàn)引入jdk時報錯
解決方法:
*把jdk最后一行暴漏方式改為export default 引入時用import就行了*
// 1、首先在需要用到的地方引入下載的jdk import QQMapWx from '../../common/qqmap-wx-jssdk.min.js' // 2、通過騰訊地圖key初始化 const qqmapSdk = new QQMapWx({ key: 'xxxxxxx' // 在騰訊地圖上申請的key }) // 3、獲取微信定位授權(quán)方法 getAuthorize () { uni.authorize({ scope: 'scope.userLocation', success: (res) => { this.getLocation() }, fail: (err) => { uni.showModal({ content: '需要授權(quán)位置信息', confirmText: '確認(rèn)授權(quán)' }).then(res => { if (res['confirm']) { uni.openSetting({ success: res => { if (res.authSetting['scope.userLocation']) { uni.showToast({ title: '授權(quán)成功', icon: 'none' }) } else { uni.showToast({ title: '授權(quán)失敗', icon: 'none' }) } this.getLocation() } }) } if (res['cancel']) { // 取消授權(quán) this.getLocation() } }) } }) } // 4、開始獲取定位方法 getLocation () { uni.getLocation({ type: 'gcj02', success: (res) => { const { latitude, longitude } = res qqmapSdk.reverseGeocoder({ location: latitude ? `${latitude},${longitude }` : '', sig: 'xxxx', // 這個sig就是上面要準(zhǔn)備的第二項SK success: (val) => { console.log('這就是要獲取的當(dāng)前所在城市的相關(guān)信息', val) }, fail: (err) => { console.log('獲取城市失敗') } }) }, fail: (err) => { if (err.errMsg === 'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF' || err.errMsg === 'getLocation:fail system permission denied') { uni.showModal({ content: '請開啟手機(jī)定位服務(wù)', showCancel: false }) } else if (err.errMsg === 'getLocation:fail:system permission denied') { uni.showModal({ content: '請給微信開啟定位權(quán)限', showCancel: false }) } } }) }
補充:UNIAPP獲取當(dāng)前城市和坐標(biāo)
uni.getLocation({ type: 'wgs84', success: res=> { console.log(res) console.log('當(dāng)前位置的經(jīng)度:' + res.longitude); console.log('當(dāng)前位置的緯度:' + res.latitude); } });
總結(jié)
到此這篇關(guān)于uniapp微信小程序獲取當(dāng)前定位城市信息的文章就介紹到這了,更多相關(guān)uniapp小程序獲取定位城市內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于微信小程序中使用wx.getLocation獲取當(dāng)前詳細(xì)位置并計算距離
這篇文章主要介紹了關(guān)于微信小程序中使用wx.getLocation獲取當(dāng)前詳細(xì)位置并計算距離,wx.getLocation只能夠獲取經(jīng)緯度,不能夠拿到詳細(xì)地址,這里使用騰訊地圖的api,需要的朋友可以參考下2023-04-04JS遍歷數(shù)組和對象的區(qū)別及遞歸遍歷對象、數(shù)組、屬性的方法詳解
本文給大家js遍歷數(shù)組和遍歷對象的區(qū)別,一般來說for用來遍歷數(shù)組對象而for-in用來遍歷非數(shù)組對象。接下來小編給大家?guī)砹薺s遍歷數(shù)組和對象的區(qū)別及js遞歸遍歷對象、數(shù)組、屬性的方法詳解,一起看下吧2016-06-06JavaScript中關(guān)于class的調(diào)用方法
下面小編就為大家?guī)硪黄琂avaScript中關(guān)于class的調(diào)用方法。具有很好的參考價值,希望對大家有所幫助2017-11-11