【uniapp】 APP&H5端-获取经纬度和地理位置信息(高德地图&腾讯地图)

1、在manifest.json文件中配置腾讯地图应用的key。

在高德地图或腾讯地图开放平台申请key

高德地图开放平台 https://console.amap.com/dev/key/app
腾讯地图开放平台 https://lbs.qq.com

在这里插入图片描述
在这里插入图片描述

2、在script中添加获取经纬度的方法:

引入weixin_sdk.js
// #ifdef H5
import wx from ‘@/utils/weixin_sdk.js’;
Vue.prototype.wx = wx;
// #endif

get_weizhi:function(){
	var that=this;
	// #ifdef H5
	var wx=this.wx;
	// 判断是否是微信公众号
	let ua = window.navigator.userAgent.toLowerCase();
	// 通过正则表达式匹配ua中是否含有MicroMessenger字符串
	if (ua.match(/MicroMessenger/i) == 'micromessenger') {
		uni.request({
			url: this.shareUrl, 
			data: {url:window.location.href},
			header: {},
			success: (r) => {
				var data=r.data;
				if(data.code==1){
					wx.config({
						debug: false, 
						appId: data.data.appId, 
						timestamp: data.data.timestamp, 
						nonceStr: data.data.nonceStr, 
						signature: data.data.signature, 
						jsApiList: ['getLocation']
					});
					wx.ready(function(){
						// 获取地理位置经纬度
						wx.getLocation({
							isHighAccuracy: true, // 开启地图精准定位
							type: 'gcj02', // 地图类型写这个
							success: (res) => {
								console.log(res)
								// Android 返回数据为"longitude":"113.67496", 
								//              "latitude":"34.752373"
								// ios 返回数据为"longitude":113.67570495605469, 
								//              "latitude":34.752342224121094
								// 将数据格式转换,并保留6位小数
								var jw_json={ 
									jingdu: parseFloat(res.longitude).toFixed(6), 
									weidu: parseFloat(res.latitude).toFixed(6)
								};
								uni.setStorageSync('jw_json', jw_json);
							},
							fail(errpr){
								uni.showToast({ title: JSON.stringify(errpr), icon: 'none' })
							}
						});
					});
				}
			}
		});
	}else{
		uni.getLocation({
		    type: 'wgs84',
		    isHighAccuracy: true,//开启高精度定位
		    success(res) {
				console.log(res)
				var jw_json={ 
					jingdu: parseFloat(res.longitude).toFixed(6), 
					weidu: parseFloat(res.latitude).toFixed(6)
				};
				uni.setStorageSync('jw_json', jw_json);
		    },
			fail: function (error) {
				console.log(error)
				that.toast(JSON.stringify(error));
			}
		})
	}
	// #endif
	// #ifdef APP-PLUS
	uni.getLocation(({
		type: "gcj02",
		altitude: true,
		isHighAccuracy: true,
		geocode: true,
		success: function (res) {
			console.log('getLocation---',res);
			var jw_json={ 
				jingdu: parseFloat(res.longitude).toFixed(6), 
				weidu: parseFloat(res.latitude).toFixed(6),
				address: res.address.district+res.address.poiName+res.address.street+res.address.streetNum
			};
			uni.setStorageSync('jw_json', jw_json);
		},
		fail: function (error) {
			console.log(error)
			this.toast(JSON.stringify(error));
		},
	}));
	// #endif
},
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值