目录
1.使用 getLocation 要配置 manifest.json ,
3. getLocation 和 openLocation 打开导航
前言
最初面向百度,遇到很多问题,过程记不太清了,其中有个报错提示让换个 API ,换了之后文档越查越多,应该是我方向错了,我只想拿到自己当前定位的经纬度,步骤越整越复杂
1.使用 getLocation 要配置 manifest.json ,
代码:
"permission": {
"scope.userLocation": {
"desc": "您的位置信息将用于附近商家位置信息的展示"
}
},
"requiredPrivateInfos": [
"getLocation",
"chooseLocation"
// "onLocationChange",
// "offLocationChange",
// "chooseAddress",
// "choosePoi",
// "getFuzzyLocation",
// "startLocationUpdate",
// "startLocationUpdateBackground"
]
2. 使用getLocation 获取定位经纬度
const updateAddress = async () => {
await new Promise<UniApp.GetLocationOptions>((resolve, reject) => {
uni.getLocation({
type: 'wgs84',
success: function (res) {
console.log(res.longitude,res.latitude, '11111111')
},
fail: function () {
uni.showToast({
title: '获取地址失败,30秒内请勿频繁获取',
icon: 'none',
})
},
})
})
}
3. getLocation 和 openLocation 打开导航
const goNavigation = async () => {
const res = await new Promise<UniApp.GetLocationOptions>((success, fail) => {
uni.getLocation({
type: 'wgs84',
geocode: true, //设置该参数为true可直接获取经纬度及城市信息
success: res => {
uni.openLocation({
latitude: res.latitude, // 纬度,范围为-90~90,负数表示南纬
longitude: res.longitude, // 经度,范围为-180~180,负数表示西经
scale: 28, // 缩放比例
name: '道院塘社区',
address: '道院塘社区华联超市',
})
console.log(res, '11111111')
},
// fail,
})
})
}