开发 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, //定位场景设置
  maxAccuracy: 0, //定位精度 单位:米
  singleLocationTimeout: 3000, //指定单次定位超时时间
  locatingWithReGeocode: false, //定位是否返回逆地理信息
  reGeocodeLanguage: AMapLocationReGeocodeLanguage.Chinese, //逆地址语言类型
  isOffset: false //是否加偏
}
3

定位信息监听 

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

开启单次定位

this.locationManger?.setLocationListener(AMapLocationType.Single,listener) //设置定位信息监听
this.locationManger?.setLocationOption(AMapLocationType.Single,options) //设置定位配置项
this.locationManger?.requestSingleLocation() //单次定位
返回顶部 示例中心 常见问题 智能客服 公众号
二维码