HarmonyOS geoLocationManager.getAddressesFromLocation将坐标转换为地理描述报错“\{code":3301300,"message":"BussinessError 3301300: Reverse geocoding query failed."\}”?

Button('转化地理位置')
  .onClick(async () => {
    console.log('进来')
    // console.log('simplt', this.simplt)
    try {
      let reverseGeocodeRequest: geoLocationManager.ReverseGeoCodeRequest =
        { "latitude": 31.12, "longitude": 121.11, "maxItems": 1 };
      //转化坐标为具体的地址描述
      let addressFromLocation = await geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest)
      console.log('getAddressesFromLocation',JSON.stringify(addressFromLocation))

    }
    catch(err){
      console.log(JSON.stringify(err))
    }
  })
})
阅读 431
1 个回答

获取位置的时候:

1.请确认下是否有配置权限module.json5中权限是否配置。

2.代码中是否已经授权打开位置获取。

3.模拟器中获取不了位置信息,请用样机测试。

可以参考demo:

// xxx.ets
import { abilityAccessCtrl, common, Permissions } from '@kit.AbilityKit';
import { geoLocationManager } from '@kit.LocationKit';

@Entry
@Component
struct Index {
  @State localText: string = '当前位置'
  permissions: Array<Permissions> = ['ohos.permission.APPROXIMATELY_LOCATION', 'ohos.permission.LOCATION'];
  getLocal() {
    const context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
    let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
    // requestPermissionsFromUser会判断权限的授权状态来决定是否唤起弹窗
    atManager.requestPermissionsFromUser(context, this.permissions).then((data) => {
      let grantStatus: Array<number> = data.authResults;
      let length: number = grantStatus.length;
      for (let i = 0; i < length; i++) {
        if (grantStatus[i] === 0) {
          // 用户授权,可以继续访问目标操作
          const requestInfo: geoLocationManager.LocationRequest = {
            'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX,
            'scenario': geoLocationManager.LocationRequestScenario.UNSET,
            'timeInterval': 1,
            'distanceInterval': 0,
            'maxAccuracy': 0
          };
          geoLocationManager.getCurrentLocation(requestInfo)
            .then((location: geoLocationManager.Location) => {
              let reverseGeocodeRequest: geoLocationManager.ReverseGeoCodeRequest = location;
              try {
                geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest).then((data) => {
                  console.log('getAddressesFromLocation: ' + JSON.stringify(data));
                  this.localText = data[0].placeName + ''
                })
                  .catch((error: number) => {
                    console.error('promise, getAddressesFromLocation: error=' + JSON.stringify(error));
                  });
              } catch (err) {
                console.error("errCode:");
              }
            })
            .catch((err: Error) => {
              console.error(`Failed to get current location. Code is , message is ${err.message}`);
            });
        } else {
          // 用户拒绝授权
          return;
        }
      }
      // 授权成功
    }).catch((err: Error) => {
      console.error(`Failed to request permissions from user. Code is , message is ${err.message}`);
    })
  }
  build() {
    Column() {
      Button(this.localText).onClick(() => {
        this.getLocal();
      })
    }
  }
}
logo
HarmonyOS
子站问答
访问
宣传栏