小程序获取用户位置 uni.getLocation

场景: 进入小程序时, 需要获取用户的位置

getLocation(){
	// 向用户发起授权请求
	uni.authorize({
		scope: "scope.userLocation", // 位置授权
		success:(res) => {
			// 发起授权成功----> 用户同意授权
			// uni.getLocation ----> 获取当前的地理位置
			uni.getLocation({
				type: 'wgs84',  // wgs84 返回 gps 坐标
				success: (res)=>{ // 同意授权 -- > 并获取成功
					// 得到 经纬度
					longitude = res.longitude
					latitude = res.latitude
					// 根据经纬度获取具体位置: 例如百度定位
				},
				fail:(err) => { // 同意授权 -- > 但是授权失败(原因可能是因为用户手机未开启定位等)
					// 调起客户端小程序设置界面
					uni.getSetting({
						success: (res) => {
							if (!res.authSetting['scope.userLocation']) {
								uni.showModal({
									title: "打开定位授权",
									success : (res) => {
										if (res.confirm) {
											uni.openSetting({
												success: (open) => {
													uni.getLocation({
														type: 'wgs84',
														success: (res)=>{
															longitude = res.longitude
															latitude = res.latitude
															// 根据经纬度获取具体位置: 例如百度定位
														}
													});
												}
											})
										} else if (res.cancel) {
										}
									}
								})
							} else {
								// 当用户手机没有开通定位功能时
								wx.showModal({
									title: '您手机定位功能没有开启',
									content: '请在系统设置中打开定位服务',
									showCancel:false,
									success : () => {
										// 提示用户开启手机定位功能
										// 进入首页
									}
								})
							}
						}
					})
				}
			})
		},
		fail: (err)=>{
			// 发起授权失败 ----> 用户拒绝授权
		}
	})
}

也可以直接用如下方法

getLocation(){
	// 向用户发起授权请求
	uni.authorize({
		scope: "scope.userLocation", // 位置授权
		success:(res) => {
			// 发起授权成功----> 用户同意授权
			// uni.getLocation ----> 获取当前的地理位置
			uni.getLocation({
				type: 'wgs84',  // wgs84 返回 gps 坐标
				success: (res)=>{ // 同意授权 -- > 并获取成功
					// 得到 经纬度
					longitude = res.longitude
					latitude = res.latitude
					// 根据经纬度获取具体位置: 例如百度定位
				},
				fail:(err) => { // 同意授权 -- > 但是授权失败(原因可能是因为用户手机未开启定位等)
					// 当用户手机没有开通定位功能时
					wx.showModal({
						title: '您手机定位功能没有开启',
						content: '请在系统设置中打开定位服务',
						showCancel:false,
						success : () => {
							// 提示用户开启手机定位功能
							// 进入首页
						}
					})
				}
			})
		},
		fail: (err)=>{
			// 发起授权失败 ----> 用户拒绝授权
		}
	})
}

若在开始进入页面时,用户拒绝定位, 后面需要重新打开小程序定位, 可用如下方法:

uni.getSetting({
	success: (result) => {
		uni.showModal({
			title: "打开定位授权",
			success : (res) => {
				if (res.confirm) {
					uni.openSetting({
						success: (open) => {
							uni.getLocation({
								type: 'wgs84',
								success: (res)=>{
									longitude = res.longitude
									latitude = res.latitude
									// 根据百度地图经纬度获取具体位置
								}
							});
						}
					})
				} else if (res.cancel) {
				}
			}
		})
	}
})
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值