欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

uniapp微信小程序獲取當(dāng)前定位城市信息的實(shí)例代碼

 更新時(shí)間:2022年08月22日 11:16:11   作者:Flyer_741  
因?yàn)閡niapp官網(wǎng)文檔的定位功能,只能提供經(jīng)緯度,如果要獲取當(dāng)前具體的位置信息,必須要調(diào)取官方的地圖方法,然后在地圖上選點(diǎn),下面這篇文章主要給大家介紹了關(guān)于uniapp微信小程序獲取當(dāng)前定位城市信息的相關(guān)資料,需要的朋友可以參考下

一、事先準(zhǔn)備

此處用的是騰訊地圖的jdk

騰訊地圖上配置

1、在騰訊地圖開發(fā)上申請(qǐng)key

2、 WebServiceAPI選擇簽名校驗(yàn)獲取SK

3、 uniapp上勾選位置接口

4、在騰訊地圖上下載微信小程序javaScript SDK放入項(xiàng)目里

二、正式代碼使用

提示:可能會(huì)出現(xiàn)引入jdk時(shí)報(bào)錯(cuò)

解決方法:

	*把jdk最后一行暴漏方式改為export default
	引入時(shí)用import就行了*
// 1、首先在需要用到的地方引入下載的jdk
import QQMapWx from '../../common/qqmap-wx-jssdk.min.js'
// 2、通過騰訊地圖key初始化
const qqmapSdk = new QQMapWx({
			key: 'xxxxxxx' // 在騰訊地圖上申請(qǐng)的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', // 這個(gè)sig就是上面要準(zhǔn)備的第二項(xiàng)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: '請(qǐng)開啟手機(jī)定位服務(wù)',
					showCancel: false
				})
			} else if (err.errMsg === 'getLocation:fail:system permission denied') {
				uni.showModal({
					content: '請(qǐng)給微信開啟定位權(quán)限',
					showCancel: false
				})
			}
		}
	})
}

補(bǔ)充: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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論