uni-app写微信小程序获取位置信息

1、调用api获取自身经纬度

uni.getLocation({
	type: 'gcj02',//腾讯地图使用gcj02获取位置坐标
	success: function (res) {
		console.log('当前位置的经度:' + res.longitude);
		console.log('当前位置的纬度:' + res.latitude);
})

2、通过经纬度获取地址信息

在腾讯位置服务注册并申请自己的key 腾讯位置开发指南
引入腾讯sdk.js 下载链接

import qqmapsdk from "@/util/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js"

uni.getLocation({
	type: 'gcj02',//腾讯地图使用gcj02获取位置坐标
	success: function (res) {
		console.log('当前位置的经度:' + res.longitude);
		console.log('当前位置的纬度:' + res.latitude);
		const   QQMapWX = new qqmapsdk({
		            //填写自己申请的Key
			        key: 'BNPBZ-JQULP-46KDJ-L7WFU-YJAGJ-ZQFLS'   
			    });
		// 解析地址
		QQMapWX.reverseGeocoder({
			location: {
				latitude: res.latitude,
				longitude: res.longitude
			},
			success: (res) => {
				//定位成功
				console.log(res.result.formatted_addresses.recommend)
			},
			fail: function(res) {
				uni.showToast({
					title: '定位失败',
					duration: 2000,
					icon: "none",
				})
			},
		})
	}
})

3、通过经纬度获取附近地址信息

let location = [res.latitude, res.longitude]
let StringLocation = location.toString()
uni.request({
	url: "https://apis.map.qq.com/ws/geocoder/v1/",
	data: {
		location: StringLocation,
		key: "BNPBZ-JQULP-46KDJ-L7WFU-YJAGJ-ZQFLS",  //填写自己申请的Key
		get_poi: 1
	},
	success: (res => {
		console.log(res.data.result.pois);
	})
})
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值