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);
})
})