微信小程序 -- 授权地理位置

获取地理位置需求,应该说是一个不太常用,但是非常常见的一个功能。

所以今天以小程序为例,一起学习一下,看看都有哪些方面需要注意。

配置manifest.json

在微信小程序配置中,必须勾选位置接口,并且写明描述

manifest.json视图

在这里插入图片描述

manifest.json源码

"mp-weixin" : {
        "appid" : "wx***xxxxx*****",
        "setting" : {
            "urlCheck" : false,
            "postcss" : true,
            "es6" : true,
            "minified" : true
        },
        "usingComponents" : true,
        "permission" : {
            "scope.userLocation" : {
                "desc" : "你的位置信息将用xxxxxxxxxxxx"
            }
        }
    },

授权请求authorize

authorize的作用,便是提前向用户发起授权请求。返回之前的授权结果:成功或者失败。一般搭配uni.getSettinguni.openSetting使用。

关于openSetting具体可以戳这里~
关于authorize具体可以戳这里~

获取经纬度 getLocation

uni.authorize({
	scope: "scope.userLocation",
	success: () => {
		uni.showLoading({
			title: '定位中...' ,
			mask:true
		})
	  uni.getLocation({
		type: "gcj02", //  wgs84: 返回GPS坐标,gcj02: 返回国测局坐标
		success: res => {
			
		  console.log(res,'经纬度')
		}
	  });
	},
	fail: () => {
		//console.log('请授权获取你的位置,否则部分功能将无法使用!')
		uni.showModal({
			//弹出提示框
			title: '是否打开设置页?',
			content: '请授权获取你的位置,否则部分功能将无法使用!',
			success(res) {
				if (res.confirm) {
					// 用户点击确定按钮
					uni.openSetting({
						// 确认后打开设置页面
						success(res) {
							// if(res.authSetting['scope.userLocation']){
							// 	console.log("手动授权成功")
							// }else{
							// 	console.log("手动授权失败")
							// }
						},
						fail(){
							console.log("打开设置页面失败")
						}
					})
				} else if (res.cancel) {
					// 用户点击取消按钮
					console.log('用户点击取消');
				}
			},
			fail(){
				console.log("确认取消弹出未弹出")
			}
		})
	},
});

到这里咱们就可以得到地理位置的经纬度,但是如果想获得详细的地理位置,还需要逆向解析一下

引入地图,逆向解析

这里咱们以小程序为例,所以引入腾讯地图。

注册账号(免费)

首先需要注册腾讯地图账号,获得key

在这里插入图片描述

创建新秘钥

在这里插入图片描述

开发者秘钥申请

在这里插入图片描述

key 设置

申请成功后,还需要详细配置一下,点击key列表设置按钮后,这里有4处重点,如图:

1、微信APP ID
2、勾选WebServiceAPI
3、选中 域名白名单可以不在里面填写域名,不然校验失败一直提示无权限。

4、安全域名设置,在小程序管理后台 -> 开发 -> 开发管理 -> 开发设置 -> “服务器域名” 中设置request合法域名,添加https://apis.map.qq.com (这里很重要)

在这里插入图片描述

下载小程序SDK

拿到秘钥后,便需要引入腾讯地图SDK,下载地址戳这里~

JavaScriptSDK v1.1
JavaScriptSDK v1.2

或者参考具体文档~

引入SDK,最终代码

addressDetailFn (){
	const qqmapsdk = new QQMapWX({ key: "Mxxx-xxx-xxxx--地图秘钥" });
	//提前获得授权结果
	uni.authorize({
		scope: "scope.userLocation",
		//已同意授权
		success: () => {
		   //开启loading,防止多次触发
			uni.showLoading({
				title: '定位中...' ,
				mask:true
			})

          //获得经纬度
		  uni.getLocation({
			type: "gcj02", //  wgs84: 返回GPS坐标,gcj02: 返回国测局坐标
			success: res => {
				
			  const {latitude, longitude} = res;
			  const location = {latitude, longitude};
			  
			  // 传入经纬度,逆向解析
			  qqmapsdk.reverseGeocoder({
				location,
				success: (res) =>{
					//关闭loading
					uni.hideLoading();
					
					//最终地址
					let ad_info = res.result.ad_info || {};
					
					let address = ad_info.province+'-'+ad_info.city+'-'+ad_info.district;
					let detailAddress = res.result.formatted_addresses.recommend
					console.log(address,'省市县')
					console.log(detailAddress,'详细的地址')
				},
				fail :(e)=>{
					uni.hideLoading();
				}
			  });
			},
			fail :(e)=>{
				//这里也很重要,有可能用户关闭了GPS,虽然可以定位,但是没有位置信息
				uni.hideLoading();
				uni.showModal({
					content:'位置定位失败,请检查您的GPS是否开启',
					showCancel:false,
					confirmText:'知道了'
				})
				console.log(e,'获取经纬度')
			}
		  });
		},
		// 拒绝了授权
		fail: () => {
			
			uni.showModal({
				//弹出提示框
				title: '是否打开设置页?',
				content: '请授权获取你的位置,否则部分功能将无法使用!',
				success(res) {
					if (res.confirm) {
						// 用户点击确定按钮
						uni.openSetting({
							// 确认后打开设置页面
							success(res) {
								// if(res.authSetting['scope.userLocation']){
								// 	console.log("手动授权成功")
								// }else{
								// 	console.log("手动授权失败")
								// }
							},
							fail(){
								console.log("打开设置页面失败")
							}
						})
					} else if (res.cancel) {
						// 用户点击取消按钮
						console.log('用户点击取消');
					}
				},
				fail(){
					console.log("确认取消弹出未弹出")
				}
			})
		},
	});
	
}

这样便能拿到详细的地理位置了,大家可能对于我拼接的最终具体地址有疑惑,这里可以根据需求自行拼接,具体参数值,戳这里~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值