HarmonyOS 内网环境定位失败

HarmonyOS
2024-12-25 16:42:48
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

定位失败是因为http请求失败,需调整内网防火墙设置。无法收到回调是由于只有在定位成功的情况下才能触发回调,可以通过geoLocationManager.on('locationError')注册定位错误的监听,并根据不同错误码执行相应的业务操作。

onForeground(): void {
  // Ability has brought to foreground
  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');

  // 增加监听定位状态改变
  let locationEnabledChange = (state:boolean):void => {
    console.log('locationEnabledChange: ' + JSON.stringify(state));
    this.context.eventHub.emit('refreshState', state)
  }
  try {
  geoLocationManager.on('locationEnabledChange', locationEnabledChange);
} catch (err) {
}
}import geoLocationManager from '@ohos.geoLocationManager';
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager';
import { BusinessError } from '@ohos.base';
import wantAgent, { WantAgent } from '@ohos.app.ability.wantAgent';
import common from '@ohos.app.ability.common';
import window from '@ohos.window';
import settings from '@ohos.settings';

/**
 * 系统定位服务实现
 */
@Entry
@Component
struct Index {
  //纬度
  @State lat: Number = -1;
  //经度
  @State lon: Number = -1;
  @State message: string = "";
  private context: Context = getContext(this);

  getLocation() {
    let requestInfo: geoLocationManager.CurrentLocationRequest = {
      'priority': geoLocationManager.LocationRequestPriority.ACCURACY,
      'scenario': geoLocationManager.LocationRequestScenario.UNSET,
      maxAccuracy: 0,
      timeoutMs: 30000

    };

    let locationChange = (err:BusinessError, location:geoLocationManager.Location):void => {
      if (err) {
        console.error('locationChanger: err=' + JSON.stringify(err));
      }
      if (location) {
        console.log('locationChanger: location=' + JSON.stringify(location));
        console.log('zzzzz :' + JSON.stringify(location))
        this.lon = location.longitude
        this.lat = location.latitude
      }
    };

    try {
      geoLocationManager.getCurrentLocation(requestInfo, locationChange);
    } catch (err) {
      console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
    }

  }

  build() {
    Column({ space: 50 }) {
      Button() {
        Text('获取权限')
          .fontSize(50)
      }
      .backgroundColor(Color.Orange)
      .width(200)
      .height(100)
      .onClick(() => {
        console.log("获取位置已点击");
        //申请权限
        let context = getContext();
        abilityAccessCtrl.createAtManager()
          .requestPermissionsFromUser(context, [
            'ohos.permission.LOCATION', 'ohos.permission.APPROXIMATELY_LOCATION',])
          .then(() => {
            this.getLocation()
          })
        console.info(`授权已成功`);
      })
      Text("当前经度为:" + this.lat);
      Text("当前纬度为:" + this.lon);
    }
    .width('100%').height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
分享
微博
QQ
微信
回复
2024-12-25 18:24:21
相关问题
内网开发环境搭建问题
1078浏览 • 1回复 待解决
HarmonyOS 定位功能定位失败问题
1588浏览 • 1回复 待解决
HarmonyOS 获取定位失败问题
936浏览 • 1回复 待解决
HarmonyOS 申请定位权限失败问题
1211浏览 • 1回复 待解决
startAbility调用失败如何定位
1483浏览 • 1回复 待解决
精度优先获取定位失败
2684浏览 • 1回复 待解决
HarmonyOS 定位失败 错误码3301200:
805浏览 • 1回复 待解决
HarmonyOS 获取定位一直失败
863浏览 • 1回复 待解决
HarmonyOS 初始化EGL环境失败
885浏览 • 1回复 待解决
Web加载失败问题定位定界指导
789浏览 • 1回复 待解决
HarmonyOS 定位服务
726浏览 • 1回复 待解决