HarmonyOS NEXT 添加地理围栏

举报
水滴石轩 发表于 2025/03/15 09:31:33 2025/03/15
【摘要】 添加一个围栏,并订阅地理围栏事件,地理围栏就是虚拟地理边界,当设备进入、离开某个特定地理区域时,可以接收自动通知和警告 目前仅支持圆形围栏,并且依赖GNSS芯片的地理围栏功能,仅在室外开阔区域才能准确识别用户进出围栏事件geofence: geoLocationManager.Geofence中的coordinateSystemType表示地理围栏圆心坐标的坐标系,APP应先使用getGeo...

添加一个围栏,并订阅地理围栏事件,地理围栏就是虚拟地理边界,当设备进入、离开某个特定地理区域时,可以接收自动通知和警告 

目前仅支持圆形围栏,并且依赖GNSS芯片的地理围栏功能,仅在室外开阔区域才能准确识别用户进出围栏事件

geofence: geoLocationManager.Geofence中的coordinateSystemType表示地理围栏圆心坐标的坐标系,APP应先使用getGeofenceSupportedCoordTypes查询支持的坐标系,然后传入正确的圆心坐标;

经纬度是围栏的中心点坐标;

radius是围栏的有效半径;

expiration是围栏的有效时间,单位是毫秒

let transitionStatusList: Array<geoLocationManager.GeofenceTransitionEvent> 是指定APP需要监听的地理围栏事件类型,这里表示需要监听进入围栏和退出围栏事件

之后构造GNSS地理围栏请求对象gnssGeofenceRequest:let gnssGeofenceRequest: geoLocationManager.GnssGeofenceRequest

将对象传入addGnssGeofence方法中就可以添加围栏了,添加成功后会返回围栏id

围栏不使用后要调用removeGnssGeofence移除围栏,避免占用性能



import { geoLocationManager } from '@kit.LocationKit';
import { BusinessError, Callback } from '@kit.BasicServicesKit';
import { promptAction } from '@kit.ArkUI';

const TAG: string = 'DDLocation';

export function gnssFenceStatusChange(geofence: geoLocationManager.Geofence,
  transitionCallback: (err : BusinessError, transition : geoLocationManager.GeofenceTransition)=>void): Promise<number> {
  return new Promise((resolve, reject) => {
    if (geofence === undefined) {
      geofence = {
        latitude: 31.871813745575167 ,
        longitude: 118.82020007987227 ,
        coordinateSystemType: geoLocationManager.CoordinateSystemType.WGS84,
        radius: 10,
        expiration: 360000,
      }
    }

    if (transitionCallback === undefined) {
      transitionCallback = (err : BusinessError, transition : geoLocationManager.GeofenceTransition) => {
        if (err) {
          console.error(TAG, 'geofenceTransitionCallback: err=' + JSON.stringify(err));
        }
        if (transition) {
          console.info(TAG, "GeofenceTransition: %{public}s", JSON.stringify(transition));
          promptAction.showToast({
            message: JSON.stringify(transition),
            duration: 5000
          })
        }
      }
    }

    let transitionStatusList: Array<geoLocationManager.GeofenceTransitionEvent> = [
      geoLocationManager.GeofenceTransitionEvent.GEOFENCE_TRANSITION_EVENT_ENTER,
      geoLocationManager.GeofenceTransitionEvent.GEOFENCE_TRANSITION_EVENT_EXIT,
    ];

    let gnssGeofenceRequest: geoLocationManager.GnssGeofenceRequest = {
      geofence: geofence,
      monitorTransitionEvents: transitionStatusList,
      geofenceTransitionCallback: transitionCallback
    }
    try {
      geoLocationManager.addGnssGeofence(gnssGeofenceRequest).then((id) => {
        console.info(TAG, "addGnssGeofence success, fence id: " + id);
        resolve(id);
      }).catch((err: BusinessError) => {
        console.error(TAG, "addGnssGeofence failed, promise errCode:" + (err as BusinessError).code +
          ",errMessage:" + (err as BusinessError).message);
        reject(err);
      });
    } catch(error) {
      console.error(TAG, "addGnssGeofence failed, err:" + JSON.stringify(error));
      reject(error);
    }
  });
}

export function removeGnssGeofence(fenceId: number): Promise<void> {
  return new Promise((resolve, reject) => {
    try {
      console.info(TAG, "removeGnssGeofence begin fenceId:" + fenceId);

      geoLocationManager.removeGnssGeofence(fenceId).then(() => {
        console.info(TAG, "removeGnssGeofence success fenceId:" + fenceId);
        resolve();
      }).catch((error : BusinessError) => {
        console.error(TAG, "removeGnssGeofence: error=" + JSON.stringify(error));
        reject(error);
      });
    } catch(error) {
      console.error(TAG, "removeGnssGeofence: error=" + JSON.stringify(error));
      reject(error);
    }
  });
}

当前api14在mate60上添加围栏时,偶现报错3301000 - The location service is unavailable;提单到华为,反馈的建议是手机要链接外网,或者插入SIM卡

我是关机重启后,链接了热点进行测试的,目前没有再复现该报错

----------------- end ---------------

后面会继续补充不足之处。

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。