uniapp 微信小程序获取位置信息

流程

在这里插入图片描述

1. 腾讯地图获取key去申请

2. 添加微信小程序的服务器域名 https://apis.map.qq.com

添加腾讯地图的域名

3. 下载qqmap-wx-jssdk.min.js下载

4. 直接上代码

	
	import QQMapWX from '@/static/js/qqmap-wx-jssdk.min.js';
	export default {
		data() {
			return {
				park: ["陕西省", "西安市", "雁塔区"],
				location:{},
			};
		},
		onLoad() {
			qqmapsdk = new QQMapWX({
				key: '自己申请的key'
			});
			console.log("load");
			this.getGeocoder()
		},
		methods:{
			getGeocoder() {
				//这个是使用自带的api获取当前经纬度后存到本地后获取
				this.location = uni.getStorageSync("location")
				
				let _this = this
				qqmapsdk.reverseGeocoder({
					location: {
						latitude: this.location.latitude,
						longitude: this.location.longitude
					},
					success(data) {
					//获取到的当前位置信息,data结果如下
						// message: "query ok"
						// request_id: "77bdb23a-054f-11ec-b2be-6c92bf5ebc0c"
						// result: {location: {…}, address: "陕西省西安市雁塔区北石桥路10118号", formatted_addresses: {…}, address_component: {…}, ad_info: {…}, …}
						// status: 0
						//解构赋值获取当前省市区
						let {
							province,
							city,
							district
						} = data.result.address_component
						_this.park = [province, city, district]
						//获取数据。。。
					},
					fail: function(error) {
						console.error(error);
					},
					complete: function(res) {
						console.log(res);
					}

				})
			},
}

完结

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值