鸿蒙NEXT开发【后台合理使用定位导航】后台任务功耗优化

在使用定位导航服务时,申请长时任务的应用应设置正确的应用场景。

约束

NA

示例

新闻类应用可以使用被动定位:

方式1:

import { geoLocationManager } from '@kit.LocationKit';

let requestInfo: geoLocationManager.LocationRequest = {
  'scenario': geoLocationManager.LocationRequestScenario.NO_POWER,
  'timeInterval': 0,
  'distanceInterval': 0,
  'maxAccuracy': 0
};

方式2:

import { geoLocationManager } from '@kit.LocationKit';

let requestInfo: geoLocationManager.LocationRequest = {
  'priority': geoLocationManager.LocationRequestPriority.LOW_POWER,
  'timeInterval': 0,
  'distanceInterval': 0,
  'maxAccuracy': 0
};

场景概述

开发者可以调用HarmonyOS位置相关接口,获取设备实时位置,或者最近的历史位置,以及监听设备的位置变化。

对于位置敏感的应用业务,建议获取设备实时位置信息。如果不需要设备实时位置信息,并且希望尽可能的节省耗电,开发者可以考虑获取最近的历史位置。

接口说明

获取设备的位置信息所使用的接口如下

表2 获取设备的位置信息接口介绍

接口名功能描述
[on(type: ‘locationChange’, request: LocationRequestContinuousLocationRequest, callback: Callback): void]
[off(type: ‘locationChange’, callback?: Callback): void]关闭位置变化订阅,并删除对应的定位请求。
[getCurrentLocation(request: CurrentLocationRequestSingleLocationRequest, callback: AsyncCallback): void]
[getCurrentLocation(request?: CurrentLocationRequestSingleLocationRequest): Promise]
[getLastLocation(): Location]获取最近一次定位结果。

开发步骤

  1. 获取设备的位置信息,需要有位置权限

  2. 导入geoLocationManager模块,所有与基础定位能力相关的功能API,都是通过该模块提供的。

    import { geoLocationManager } from '@kit.LocationKit';
    
  3. 单次获取当前设备位置。多用于查看当前位置、签到打卡、服务推荐等场景。

    • 方式一:获取系统缓存的最新位置。

      如果系统当前没有缓存位置会返回错误码。

      推荐优先使用该接口获取位置,可以减少系统功耗。

      如果对位置的新鲜度比较敏感,可以先获取缓存位置,将位置中的时间戳与当前时间对比,若新鲜度不满足预期可以使用方式二获取位置。

import { geoLocationManager } from '@kit.LocationKit';
import { BusinessError } from '@kit.BasicServicesKit'
try {
    let location = geoLocationManager.getLastLocation();
} catch (err) {
    console.error("errCode:" + JSON.stringify(err));
}
  • 方式二:获取当前位置。

首先要实例化[SingleLocationRequest]对象,用于告知系统该向应用提供何种类型的位置服务,以及单次定位超时时间。

  • 设置LocatingPriority:

如果对位置的返回精度要求较高,建议LocatingPriority参数优先选择PRIORITY_ACCURACY,会将一段时间内精度较好的结果返回给应用。

如果对定位速度要求较高,建议LocatingPriority参数选择PRIORITY_LOCATING_SPEED,会将最先拿到的定位结果返回给应用。

两种定位策略均会同时使用GNSS定位和网络定位技术,以便在室内和户外场景下均可以获取到位置结果,对设备的硬件资源消耗较大,功耗也较大。

  • 设置locatingTimeoutMs:

因为设备环境、设备所处状态、系统功耗管控策略等的影响,定位返回的时延会有较大波动,建议把单次定位超时时间设置为10秒。

以快速定位策略(PRIORITY_LOCATING_SPEED)为例,调用方式如下:

import { geoLocationManager } from '@kit.LocationKit';
import { BusinessError } from '@kit.BasicServicesKit'
let request: geoLocationManager.SingleLocationRequest = {
   'locatingPriority': geoLocationManager.LocatingPriority.PRIORITY_LOCATING_SPEED,
   'locatingTimeoutMs': 10000
}
try {
   geoLocationManager.getCurrentLocation(request).then((result) => { // 调用getCurrentLocation获取当前设备位置,通过promise接收上报的位置
      console.log('current location: ' + JSON.stringify(result));
   })
   .catch((error:BusinessError) => { // 接收上报的错误码
      console.error('promise, getCurrentLocation: error=' + JSON.stringify(error));
   });
 } catch (err) {
   console.error("errCode:" + JSON.stringify(err));
 }
  1. 持续定位。多用于导航、运动轨迹、出行等场景。

首先要实例化[ContinuousLocationRequest]对象,用于告知系统该向应用提供何种类型的位置服务,以及位置结果上报的频率。

  • 设置locationScenario:

建议locationScenario参数优先根据应用的使用场景进行设置,例如地图在导航时使用NAVIGATION参数,可以持续在室内和室外场景获取位置用于导航。

  • 设置interval:

表示上报位置信息的时间间隔,单位是秒,默认值为1秒。如果对位置上报时间间隔无特殊要求,可以不填写该字段。

以地图导航场景为例,调用方式如下:

import { geoLocationManager } from '@kit.LocationKit';
let request: geoLocationManager.ContinuousLocationRequest= {
   'interval': 1,
   'locationScenario': geoLocationManager.UserActivityScenario.NAVIGATION
}
let locationCallback = (location:geoLocationManager.Location):void => {
   console.log('locationCallback: data: ' + JSON.stringify(location));
};
try {
   geoLocationManager.on('locationChange', request, locationCallback);
} catch (err) {
   console.error("errCode:" + JSON.stringify(err));
}

如果不主动结束定位可能导致设备功耗高,耗电快;建议在不需要获取定位信息时及时结束定位。

geoLocationManager.off('locationChange', locationCallback);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值