HarmonyOS 鸿蒙Next逆地理编码获取不到地区邮编

发布于 1周前 作者 yibo5220 来自 鸿蒙OS

HarmonyOS 鸿蒙Next逆地理编码获取不到地区邮编

import { geoLocationManager } from ‘@kit.LocationKit’;
import { abilityAccessCtrl, common } from ‘@kit.AbilityKit’;

async function getLocation(
//经纬度
setLocation: (location: string) => void,
//countryName:国家,
// administrativeArea:省份,
// locality:市区,
// subLocality:县区

setAddressInfo: (
countryName: string,
administrativeArea: string,
locality: string,
subLocality: string,
placeName:string,
postalCode:string,
) => void
) {
let context = getContext() as common.UIAbilityContext;
let atManager = abilityAccessCtrl.createAtManager();
await atManager.requestPermissionsFromUser(context, [‘ohos.permission.APPROXIMATELY_LOCATION’]);

let latitude: number = 0;
let longitude: number = 0;
let requestInfo: geoLocationManager.SingleLocationRequest = {
locatingTimeoutMs:3000,
locatingPriority:geoLocationManager.LocatingPriority.PRIORITY_ACCURACY
// ‘priority’: geoLocationManager.LocationRequestPriority.ACCURACY,
// ‘scenario’: geoLocationManager.LocationRequestScenario.UNSET,
};

try {
let locationResult = await geoLocationManager.getCurrentLocation(requestInfo);
console.log(‘current location: ’ + JSON.stringify(locationResult));
setLocation(locationResult.latitude + ‘, ’ + locationResult.longitude);
latitude = locationResult.latitude;
longitude = locationResult.longitude;
} catch (error) {
console.error(‘promise, getCurrentLocation: error=’ + JSON.stringify(error));
setLocation(‘Error fetching location’);
return;
}

let reverseGeocodeRequest: geoLocationManager.ReverseGeoCodeRequest = {
“locale”: “zh”,
“latitude”: latitude,
“longitude”: longitude,
“maxItems”: 1
};

try {
let addressResult = await geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest);
console.log(‘getAddressesFromLocation data: ’ + JSON.stringify(addressResult));
let address = addressResult[0];
setAddressInfo(
address.countryName as string,
address.administrativeArea as string,
address.locality as string,
address.subLocality as string,
address.placeName as string,
address.postalCode as string
);
} catch (error) {
console.log(‘promise, getAddressesFromLocation: error=’ + JSON.stringify(error));
}
}

@Entry
@Component
struct location {
@State string: string = ‘’
@State mCountryName: string = ‘’ // 国家名称
@State mAdministrativeArea: string = ‘’ // 省份
@State mLocality: string = ‘’ // 地市
@State mSubLocality: string = ‘’ // 县区
@State placeName:string=’’
@State postalCode:string=’’

aboutToAppear(): void {
getLocation(
(location) => {
this.string = location;
},
(countryName, administrativeArea, locality, subLocality,placeName,postalCode) => {
this.mCountryName = countryName;
this.mAdministrativeArea = administrativeArea;
this.mLocality = locality;
this.mSubLocality = subLocality;
this.placeName = placeName;
this.postalCode=postalCode;
}
);
}

build() {
Column() {
Text(this.string)
Text(‘国家:’+this.mCountryName)
Text(‘省份:’ +this.mAdministrativeArea)
Text(‘地市:’+ this.mLocality)
Text(‘县区:’+this.mSubLocality)
Text(this.placeName)
Text(this.postalCode)
}
}
}

4 回复
楼主您好,可以尝试将LocationRequestPriority、LocationRequestScenario分别设置为ACCURACY、NAVIGATION之后 再进行尝试后看是否能获取邮编
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-geolocationmanager-V5#locationrequestscenario

你好,谢谢回答,我问了华为那边说是自带的地图数据库里本身没有存编码数据,谢谢回答

有没有大佬知道这为什么获取不到地区的邮编。

HarmonyOS 鸿蒙Next逆地理编码获取不到地区邮编,可能是由于服务未启用、权限未授予、网络问题或数据不准确等原因。请确保地理编码服务已启用,并检查应用的定位和网络权限。同时,确认输入的经纬度数据准确无误。如果问题依旧无法解决,请检查官方文档或更新系统至最新版本。如果问题依旧没法解决请加我微信,我的微信是itying888。

回到顶部