开发 HarmonyOS NEXT 定位 SDK 开发指南 连续定位

连续定位 最后更新时间: 2024年04月25日

在使用连续定位功能之前,请务必确保您已按照 工程配置 完成了所有必要的配置步骤,并已获取了相应的权限

提示

连续定位功能需近似位置、精确位置、网络访问权限

开始使用连续定位

1

导入所需模块

import { AMapLocationOption, AMapLocationReGeocodeLanguage, AMapLocationType, IAMapLocationListener } from '@amap/amap_lbs_location';
import geoLocationManager from '@ohos.geoLocationManager';
2

定位参数配置

let options: AMapLocationOption = {
  priority: geoLocationManager.LocationRequestPriority.FIRST_FIX, //定位优先配置选项
  scenario: geoLocationManager.LocationRequestScenario.UNSET, //定位场景设置
  timeInterval: 2, //定位时间间隔
  distanceInterval: 0, //位置报告距离间隔
  maxAccuracy: 20, //定位精度 单位:米
  allowsBackgroundLocationUpdates: false, //是否允许后台定位
  locatingWithReGeocode: true, //定位是否返回逆地理信息
  reGeocodeLanguage: AMapLocationReGeocodeLanguage.Chinese, //逆地址语言类型
  isOffset: true //是否加偏
}
3

定位信息监听 

let listener: IAMapLocationListener = {
  onLocationChanged: (location) => {
    // todo something
  }, onLocationError: (error) => {
    // todo something
  }
};
4

开启连续定位

this.locationManger?.setLocationListener(AMapLocationType.Updating, listener) //设置定位信息监听
this.locationManger?.setLocationOption(AMapLocationType.Updating, options) //设置定位配置项
this.locationManger?.startUpdatingLocation() //开启连续定位

// this.locationManger?.stopUpdatingLocation() //关闭连续定位
提示

如需在后台定位(后台定位需后台定位后台运行权限),则要在应用切入后台前调用开启后台定位任务,

this.locationManger?.startContinuousTask() //开启后台定位任务

回到前台可以停止后台任务

this.locationManger?.stopContinuousTask() //停止后台定位任务

返回顶部 示例中心 常见问题 智能客服 公众号
二维码