- 发布:2022-07-05 12:02
- 更新:2023-05-04 17:38
- 阅读:723
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: win10
HBuilderX类型: 正式
HBuilderX版本号: 3.4.18
手机系统: 全部
手机系统版本号: Android 12
手机厂商: 华为
手机机型: 华为P40pro
页面类型: vue
vue版本: vue2
打包方式: 云端
项目创建方式: HBuilderX
测试过的手机:
示例代码:
export function getLocations(type) {
return new Promise((resolve, reject) => {
uni.getLocation({
type: type ? type : 'wgs84', // type 'gcj02' 'wgs84'
geocode: true, //设置该参数为true可直接获取经纬度及城市信息
success: (res) => {
resolve(res)
},
fail: (err) => {
resolve(err)
}
});
})
}
export function getLocations(type) {
return new Promise((resolve, reject) => {
uni.getLocation({
type: type ? type : 'wgs84', // type 'gcj02' 'wgs84'
geocode: true, //设置该参数为true可直接获取经纬度及城市信息
success: (res) => {
resolve(res)
},
fail: (err) => {
resolve(err)
}
});
})
}
操作步骤:
async getAddres(){
let that = this
let location = await getLocations()
console.log("locationlocation",location)
if(location){
that.queryParams.latitude = location.latitude
that.queryParams.longitude = location.longitude
// output=JSON&location=104.027614,30.611182&key=ba145b6aed789b56a94e867d41cead63
let url = configService.gaodeMapUrl + '/v3/geocode/regeo?output=JSON&location=' + location.longitude +','+location.latitude+'&key='+configService.gaodeWebKey
console.log("url--",url)
uni.request({
url: url,
success: (res) => {
console.log("Resss--",res)
if (res && res.data) {
let addressData = res.data.regeocode
// that.queryParams.addr =res.data.regeocode.formatted_address
that.queryParams.addr = `${addressData.addressComponent.province?addressData.addressComponent.province:''}${addressData.addressComponent.city?addressData.addressComponent.city:''}${addressData.addressComponent.district?addressData.addressComponent.district:''}${addressData.addressComponent.township?addressData.addressComponent.township:''}${addressData.addressComponent.streetNumber &&addressData.addressComponent.streetNumber.street ?addressData.addressComponent.streetNumber.street:''}`
}
}
})
// if(location.address){
// that.queryParams.addr = `${location.address.country?location.address.country:''}${location.address.province?location.address.province:''}${location.address.city?location.address.city:''}${location.address.district?location.address.district:''}${location.address.street?location.address.street:''}${location.address.streetNum?location.address.streetNum:''}${location.address.poiName?location.address.poiName:''}`
// }
}
},
async getAddres(){
let that = this
let location = await getLocations()
console.log("locationlocation",location)
if(location){
that.queryParams.latitude = location.latitude
that.queryParams.longitude = location.longitude
// output=JSON&location=104.027614,30.611182&key=ba145b6aed789b56a94e867d41cead63
let url = configService.gaodeMapUrl + '/v3/geocode/regeo?output=JSON&location=' + location.longitude +','+location.latitude+'&key='+configService.gaodeWebKey
console.log("url--",url)
uni.request({
url: url,
success: (res) => {
console.log("Resss--",res)
if (res && res.data) {
let addressData = res.data.regeocode
// that.queryParams.addr =res.data.regeocode.formatted_address
that.queryParams.addr = `${addressData.addressComponent.province?addressData.addressComponent.province:''}${addressData.addressComponent.city?addressData.addressComponent.city:''}${addressData.addressComponent.district?addressData.addressComponent.district:''}${addressData.addressComponent.township?addressData.addressComponent.township:''}${addressData.addressComponent.streetNumber &&addressData.addressComponent.streetNumber.street ?addressData.addressComponent.streetNumber.street:''}`
}
}
})
// if(location.address){
// that.queryParams.addr = `${location.address.country?location.address.country:''}${location.address.province?location.address.province:''}${location.address.city?location.address.city:''}${location.address.district?location.address.district:''}${location.address.street?location.address.street:''}${location.address.streetNum?location.address.streetNum:''}${location.address.poiName?location.address.poiName:''}`
// }
}
},
预期结果:
获取到经纬度,反查位置信息
获取到经纬度,反查位置信息
实际结果:
未获取到经纬度也未报错
未获取到经纬度也未报错
bug描述:
获取定位没有反应,正确的和错误的都没返回
小嗨嗨 (作者)
我用高德地图api把经纬度逆解析出地址,有点不太准
2023-03-27 14:44
5***@qq.com
回复 小嗨嗨: 同问,找到原因了吗,最后怎么解决的?华为鸿蒙uni.getLocation 无响应。
2023-04-29 12:38