uniapp微信小程序获取当前定位

一、申请密钥key

  1. https://lbs.qq.com/ 腾讯申请地址,没注册的先注册,然后进入控制台
  2. 创建应用,添加key在这里插入图片描述
  3. uniapp授权在这里插入图片描述
  4. 在腾讯地图上下载微信小程序javaScript SDK放入项目里,地址https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/jsSdkOverview在这里插入图片描述
  5. 引入项目,路径自行改在这里插入图片描述

二、项目使用代码

提示,引入可能会报错,修改如下

在这里插入图片描述

代码使用

    // 1、引入SDK核心类,js文件根据自己业务,位置可自行放置
	import QQMapWx from '@/js_sdk/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.js'
	// 2、实例化API核心类
	const qqmapSdk = new QQMapWx({
		key: '*********'
	});
	// 3、获取微信定位授权方法
			getAuthorize() {
				// #ifdef MP-WEIXIN
				uni.authorize({
					scope: 'scope.userLocation',
					success: (res) => {
						this.getLocation('gcj02');
					},
					fail: (err) => {
						uni.showModal({
							content: '需要授权位置信息',
							confirmText: '确认授权'
						}).then(res => {
							if (res['confirm']) {
								uni.openSetting({
									success: res => {
										if (res.authSetting['scope.userLocation']) {
											uni.showToast({
												title: '授权成功',
												icon: 'none'
											})
										} else {
											uni.showToast({
												title: '授权失败',
												icon: 'none'
											})
										}
										this.getLocation('gcj02')
									}
								})
							}
							if (res['cancel']) {
								// 取消授权
								this.getLocation('gcj02')
							}
						})
					}
				})
				// #endif
				// this.getLocation('wgs84');
			},
	// 4、开始获取定位方法
			getLocation(type) {
				uni.getLocation({
					type: type,
					// geocode:true,//设置该参数为true可直接获取经纬度及城市信息
					success: (res) => {
						
						const {
							latitude,
							longitude
						} = res;


						
						qqmapSdk.reverseGeocoder({
							location: latitude ? `${latitude},${longitude }` : '',
							sig: '**************', // 这个sig就是上面要准备的第二项SK
							success: (val) => {
								console.log('这就是要获取的当前所在城市的相关信息', val)
								
							},
							fail: (err) => {
								console.log('获取城市失败')
							}
						})
					},
					fail: (err) => {
						uni.stopPullDownRefresh();
						if (err.errMsg === 'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF' || err
							.errMsg === 'getLocation:fail system permission denied') {
							uni.showModal({
								content: '请开启手机定位服务',
								showCancel: false
							})
						} else if (err.errMsg === 'getLocation:fail:system permission denied') {
							uni.showModal({
								content: '请给微信开启定位权限',
								showCancel: false
							})
						}
					}
				})
			},

over

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在uniapp微信小程序获取手机号,可以按照以下步骤进行操作: 1. 用户获取code:用户需要调用login方法获取code。可以在onLoad()生命周期函数中调用getuserNew()方法来获取code。代码示例如下: ``` onLoad() { this.getuserNew(); // 获取openid }, methods: { // 获取code getuserNew() { uni.login({ provider: 'weixin', success: res => { console.log(res.code); } }); }, } ``` 2. 通过微信官方接口获取openId和session_key:使用uni.request()方法来向微信官方接口发送获取openId和session_key的请求。代码示例如下: ``` uni.request({ url: 'https://api.weixin.qq.com/sns/jscode2session', method: 'GET', data: { appid: 'wx9*******214e0', //你的小程序APPID secret: '33c4d17e4********71253', //你的小程序的secret js_code: res.code, //wx.login登录成功后的code grant_type: 'authorization_code', }, success: (cts) => { // 换取成功后,暂存这些数据留作后续操作 this.openId = cts.data.openid; //openid 用户唯一标识 this.session_key = cts.data.session_key; //session_key 会话密钥 console.log(cts); console.log(this.openId, this.session_key); } }); ``` 3. 获取手机号码权限并传递相关信息:在前端进行手机号码授权,并将encryptedData、iv、sessionKey和openId这四个字段传递给后端。后端通过用户信息授权码获取openid,通过手机授权码获取手机号码。具体细节可以根据你的需求和后端逻辑进行调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [uniapp 微信小程序 获取手机号/用户授权(最详细)](https://blog.csdn.net/qq_42957741/article/details/117086865)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [uniapp微信小程序授权登录并获取手机号](https://blog.csdn.net/mrqi122/article/details/129520675)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值