uniapp小程序接入地图实现定位

文章讲述了在微信小程序中如何通过`uni.chooseLocation`和`AMapWX.getRegeo`获取用户地理位置,以及在无权限时如何提示用户授权并处理授权流程的过程。
摘要由CSDN通过智能技术生成
getLocation() {
				uni.chooseLocation({
					success: (res) => {
						this.form.address = res.name
					},
					fail: () => {
						// 如果用uni.chooseLocation没有获取到地理位置,则需要获取当前的授权信息,判断是否有地理授权信息
						uni.getSetting({
							success: (res) => {
								var status = res.authSetting;
								if (!status['scope.userLocation']) {
									// 如果授权信息中没有地理位置的授权,则需要弹窗提示用户需要授权地理信息
									uni.showModal({
										title: "是否授权当前位置",
										content: "需要获取您的地理位置,请确认授权,否则地图功能将无法使用",
										success: (tip) => {
											if (tip.confirm) {
												// 如果用户同意授权地理信息,则打开授权设置页面,判断用户的操作
												uni.openSetting({
													success: (data) => {
														// 如果用户授权了地理信息在,则提示授权成功
														if (data
															.authSetting[
																'scope.userLocation'
															] === true
														) {
															uni.showToast({
																title: "授权成功",
																icon: "success",
																duration: 1000
															})
															// 授权成功后,然后再次chooseLocation获取信息
															uni.chooseLocation({
																success: (
																	res
																) => {
																	this.form
																		.address =
																		res
																		.name
																}
															})
														} else {
															uni.showToast({
																title: "授权失败",
																icon: "none",
																duration: 1000
															})
														}
													}
												})
											}
										}
									})
								}
							},
							fail: (res) => {
								uni.showToast({
									title: "调用授权窗口失败",
									icon: "none",
									duration: 1000
								})
							}
						})
					}
				});
			},

			getRegeo() {
				const _this = this;
				uni.showLoading({
					title: '获取地址信息中'
				});
				_this.amapPlugin.getRegeo({
					success: (data) => {
						_this.form.address = data[0].name;
						uni.hideLoading();
					},
					fail: (err) => {
						uni.getSetting({
							success: (res) => {
								console.log(res);
								var status = res.authSetting;
								if (!status['scope.userLocation']) {
									// 如果授权信息中没有地理位置的授权,则需要弹窗提示用户需要授权地理信息
									uni.showModal({
										title: "是否授权当前位置",
										content: "需要获取您的地理位置,请确认授权,否则地图功能将无法使用",
										success: (tip) => {
											if (tip.confirm) {
												// 如果用户同意授权地理信息,则打开授权设置页面,判断用户的操作
												uni.openSetting({
													success: (data) => {
														// 如果用户授权了地理信息在,则提示授权成功
														if (data
															.authSetting[
																'scope.userLocation'
															] === true
														) {
															uni.showToast({
																title: "授权成功",
																icon: "success",
																duration: 1000
															})
															// 授权成功后,然后再次chooseLocation获取信息
															_this
																.amapPlugin
																.getRegeo({
																	success: (
																		data
																	) => {
																		_this
																			.form
																			.address =
																			data[
																				0
																			]
																			.name;
																		uni
																			.hideLoading();
																	},
																})
														} else {
															uni.showToast({
																title: "授权失败",
																icon: "none",
																duration: 1000
															})
														}
													}
												})
											}
										}
									})
								}
							},
							fail: (res) => {
								uni.showToast({
									title: "调用授权窗口失败",
									icon: "none",
									duration: 1000
								})
							}
						})
					}
				});
			},
onLoad() {
			
			this.amapPlugin = new amap.AMapWX({
				key: this.key
			})
			this.getRegeo()

		},

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值