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

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容