uniapp小程序获取经纬度方法封装

1.manifest.json配置

/* 小程序特有相关 */
	"mp-weixin": {
		"appid": "wx735**********9b3f",
		"permission": {
			"scope.userLocation": {
				"desc": "你的位置信息将用于小程序位置接口的效果展示"
			}
		},
		"requiredPrivateInfos": ["getLocation"]
	},

2.页面使用  直接cv  在fnGetlocation()方法内操作返回的经纬度数据

// 定位授权
			getLocation() {
				let that = this;
				// 判断手机定位服务【GPS】 是否授权
				uni.getSystemInfo({
					success(res) {
						let locationEnabled = res.locationEnabled; //判断手机定位服务是否开启
						let locationAuthorized = res.locationAuthorized; //判断定位服务是否允许微信授权
						if (locationEnabled == false || locationAuthorized == false) {
							//手机定位服务(GPS)未授权
							that.$common.toast('请打开手机GPS');
						} else {
							//手机定位服务(GPS)已授权
							//判断微信小程序是否授权位置信息
							uni.authorize({
								scope: 'scope.userLocation',
								success: res => {
									that.fnGetlocation();
								},
								fail: err => {
									err = err['errMsg'];
									uni.showModal({
										content: '需要授权位置信息',
										confirmText: '确认授权'
									}).then(res => {
										console.log(res);
										if (res[1]['confirm']) {
											uni.openSetting({
												success: res => {
													if (res.authSetting[
															'scope.userLocation'
														]) {
														that.$common.toast('授权成功');
														that.fnGetlocation();
													} else {
														// 未授权
														that.$common.toast(
															'授权失败,请重新授权');
														uni.showModal({
															title: '授权',
															content: '获取授权' +
																authouName +
																'失败,是否前往授权设置?',
															success: function(
																result
															) {
																if (result
																	.confirm
																) {
																	uni
																		.openSetting();
																}
															},
															fail: function() {
																that.$common
																	.toast(
																		'系统错误!'
																	);
															}
														});
													}
												}
											});
										}
										if (res[1]['cancel']) {
											// 取消授权
											that.$common.toast('你拒绝了授权,无法获得周边信息');
										}
									});
								}
							});
						}
					}
				});
			},
			// 定位获取
			fnGetlocation() {
				let that = this;
				uni.getLocation({
					type: 'wgs84', //返回 gps 坐标
					geocode: 'true',
					isHighAccuracy: 'true',
					accuracy: 'best', // 精度值为20m
					success: function(res) {
                         // 在这里操作经纬度数据!!!!!!!!!!
						that.bindList.long = res.longitude;
						that.bindList.lat = res.latitude;
						uni.setStorageSync('longlat', res.longitude + ',' + res.latitude);
						console.log('自动定位' + res.errMsg);
                       
					},
					fail(err) {
						if (err.errMsg === 'getLocation:fail 频繁调用会增加电量损耗,可考虑使用 wx.onLocationChange 监听地理位置变化') {
							// that.$common.toast('请勿频繁定位')
						}
						if (err.errMsg === 'getLocation:fail auth deny') {
							// 未授权
							that.$common.toast('无法定位,请重新获取位置信息');
							authDenyCb && authDenyCb();
							that.isLocated = false;
						}
						if (err.errMsg === 'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF') {
							uni.showModal({
								content: '请开启手机定位服务',
								showCancel: false
							});
						}
					}
				});
			}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值