在使用 uni.getLocation 步骤和一些坑
1.使用前需要去微信小程序申请开通wx.getLocation 获取当前位置的api (不申请上线之后会使用不了),申报资料的话我让ai帮我写的,说一下使用场景弄几张图片就可以了
地址 : 小程序
2.第二个需要配置域名
具体申请腾讯地图的方法参考 :uniapp开发小程序获取定位信息(腾讯地图)_uniapp 定位-CSDN博客
其他资料网上都有的,说一下我遇见的问题,uni.getLocation 微信小程序上线之后只有苹果手机可以使用,安卓手机获取不到位置,然后也没有进 success fail, 就挺奇怪的,在网上找了一些方法都不是,还是没有解决。
最后没办法只能换个方法实现了,准备使用 wx.chooseLocation的时候才发现原来是点击的时候没有生效,并不是uni.getLocation不行,后面将图标的权重加大就可以了,但是不知道为什么苹果手机可以安卓不行,知道的可以解答一下
代码参考:
//获取地理位置
import QQMapWX from "@/utils/qqmap-wx-jssdk.js"
const getLocationInfo = {
data() {
return {
showLocation: false
};
},
methods: {
// 获取腾讯地图位置
async getLocationInfo() {
return new Promise((resolve) => {
let location = {
longitude: 0,
latitude: 0,
province: "",
city: "",
area: "",
street: "",
showLocation: true,
address: "",
formatted_addresses: ""
};
// 使用uni.getLocation获取经纬度
uni.getLocation({
type: "gcj02",
isHighAccuracy: false,
success(res) {
location.longitude = res.longitude;
location.latitude = res.latitude;
// 创建实例
const qqmapsdk = new QQMapWX({
key: 'MPEBZ-BQR6Z-VLIXI-ZGIFB-Z5UZ6-BVB99' //这里填写自己申请的key
});
// 使用腾讯的逆地址解析
qqmapsdk.reverseGeocoder({
location,
success(response) {
console.log("成功!", response.result)
let info = response.result;
console.log(info);
location.province = info.address_component.province;
location.city = info.address_component.city;
location.area = info.address_component.district;
location.street = info.address_component.street;
location.address = info.address;
location.formatted_addresses = info.formatted_addresses
.standard_address;
resolve(location);
},
});
},
fail(err) {
console.log(err)
uni.showToast({
title: '获取位置失败,请重试',
icon: 'none'
});
resolve(location);
},
});
});
}
}
};
export default getLocationInfo;