HarmonyOS 内网环境定位失败

HarmonyOS
1天前
浏览
收藏 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%')
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
内网开发环境搭建问题
212浏览 • 1回复 待解决
HarmonyOS 获取定位失败问题
41浏览 • 1回复 待解决
startAbility调用失败如何定位
819浏览 • 1回复 待解决
精度优先获取定位失败
1759浏览 • 1回复 待解决
HarmonyOS 定位服务
51浏览 • 1回复 待解决
HarmonyOS 环境设置
92浏览 • 1回复 待解决
HarmonyOS 系统环境检测
307浏览 • 1回复 待解决
HarmonyOS 定位服务、地图服务
44浏览 • 1回复 待解决