微信小程序 获取地理位置使用

微信小程序 现在 的 地图定位是需要申请的。
	import amapFile from "../../common/amap-wx.130";     // 引入微信地图的SDK 
getLocation(lat = '', lon = '', addr = '') {
				console.log('getLocationgetLocation')
				// if (addr == '未知地址') {  // 微信获取地理位置的方法。
					uni.authorize({
						scope: 'scope.userLocation',
						success() {
							console.log('用户未开启权限')
							var myAmapFun = new amapFile.AMapWX({
								key: '9e3b273b3a6b2792065fba31a74a534e'
							});
							uni.getLocation({
								type: 'gcj02',
								success: function(res) {
									console.log('当前位置的经度:' + res.longitude);
									console.log('当前位置的纬度:' + res.latitude);
									lat = res.latitude;
									lon = res.longitude;
									console.log(lat, lon, addr)
									// check token
									myAmapFun.getRegeo({
										location: `${lon},${lat}`,
										success(data) {
											let str = {
												province: '',
												city: ''
											}
											// 拿到省份
											str.province = data[0].regeocodeData
												.addressComponent.province;
											// 拿到城市
											str.city = data[0].regeocodeData.addressComponent
												.city;
											if (Object.prototype.toString.call(str.city) ==
												"[object Array]") {
												str.city = str.city.join('');
											}
											addr = data[0].regeocodeData.formatted_address;
											console.log();
											wx.openLocation({
												latitude: lat, // 纬度,范围为-90~90,负数表示南纬
												longitude: lon, // 经度,范围为-180~180,负数表示西经
												scale: 15, // 缩放比例
												address: addr ? addr : '',
												success: function(r) {
													console.log(r)
												}
											})
										},
										fail(err) {
											console.log('333333333333333', err)
										}
									})
								}
							});
			
						},
						fail(e) {
							uni.showToast({
								title: '用户未开启权限',
								icon: 'none'
							})
							setTimeout(() => {
								uni.openSetting({
									success(res) {
										console.log(res.authSetting)
									}
								 })
							}, 1000)
						 console.log(e);
						    // 判断用户是否拒绝了授权
						 //  wx.getSetting({
						 //       success: res => {
						 //         if (typeof(res.authSetting['scope.userLocation']) != 'undefined' && !res.authSetting['scope.userLocation']) {
						 //           // 用户拒绝了授权
						 //           wx.showModal({
						 //             title: '提示',
						 //             content: '您拒绝了定位权限,将无法使用XX功能',
						 //             success: res => {
						 //               if (res.confirm) {
						 //                 // 跳转设置页面
						 //                 wx.openSetting({
						 //                   success: res => {
						 //                     if (res.authSetting['scope.userLocation']) {
						 //                       // 授权成功,重新定位
						 //                       wx.getLocation({
						 //                         success: res => {}
						 //                       });
						 //                     } else {
						 //                       // 没有允许定位权限
						 //                       wx.showToast({
						 //                         title: '您拒绝了定位权限,将无法使用XX功能',
						 //                         icon: 'none'
						 //                       });
						 //                     }
						 //                   }
						 //                 });
						 //               }
						 //             }
						 //           });
							// 	}}
							// })
						}
					})
				// } else {
				// 	console.log(lat, lon, addr)
				// 	wx.openLocation({
				// 		latitude: lat, // 纬度,范围为-90~90,负数表示南纬
				// 		longitude: lon, // 经度,范围为-180~180,负数表示西经
				// 		scale: 15, // 缩放比例
				// 		address: addr ? addr : '',
				// 		success: function(r) {
				// 			console.log(r)
				// 		}
				// 	})
				// }
			},



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值