HarmonyOS NEXT 添加地理围栏

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

目前仅支持圆形围栏,并且依赖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 **********';
import { BusinessError, Callback } from **********';
import { promptAction } from **********';

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 ---------------

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

#商务##Ark-TS语言#
全部评论

相关推荐

11-13 14:37
门头沟学院 Java
程序员牛肉:是的,我觉得你最先需要的是多接触计算机圈子。我感觉你这个写的太幼稚了,根本没换位思考面试官。 你对实习的描述还是我写了前后端,我写了Restful接口,我用了EChatrs。你这让面试官怎么问你?问你什么是前后端?问你什么是Restful?讲真的兄弟,你这个简历在面试官眼里就是啥也不懂的好学生。所以一定要尽快加入一个圈子跟大家多聊聊,看看正儿八经的简历是怎么写的。 可以看一下我首页的简历怎么写那篇文章来学一下,你这里面的坑点我那篇文章里面都有讲过。
点赞 评论 收藏
分享
12-15 11:27
门头沟学院 Java
哇哇的菜鸡oc:所有人不要理会,就好了,后面他就知道怎么回事了,只能说有的时候市场都是被宰的人搞坏的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务