TencentLBSGeofenceManager Class Reference

Inherits from NSObject
Declared in TencentLBSGeofenceManager.h

Overview

腾讯地理围栏管理类,提供围栏的创建、监控和管理功能。 支持圆形围栏和多边形围栏,可监听进入、离开和停留事件。

使用步骤: 1. 创建 TencentLBSGeofenceManager 实例并设置 delegate 2. 设置 coordinateType 指定围栏坐标系(默认 GCJ-02,与腾讯地图一致) 3. 创建围栏对象,设置 activeAction(监听事件类型,如 Enter/Exit/Stayed)和 stayedDuration(停留阈值)等围栏级别属性 4. 调用 addGeofenceRegion: 添加围栏(坐标系需与 coordinateType 一致) 5. 在代理回调中处理围栏事件

  delegate

实现了 TencentLBSGeofenceManagerDelegate 协议的代理对象

@property (nonatomic, weak, nullable) id<TencentLBSGeofenceManagerDelegate> delegate

Declared In

TencentLBSGeofenceManager.h

  allowsBackgroundLocationUpdates

是否允许后台位置更新,默认为 NO

@property (nonatomic, assign) BOOL allowsBackgroundLocationUpdates

Declared In

TencentLBSGeofenceManager.h

  geofenceRegions

当前已添加的所有围栏区域

@property (nonatomic, copy, readonly) NSArray<TencentLBSGeofenceRegion*> *geofenceRegions

Declared In

TencentLBSGeofenceManager.h

  desiredAccuracy

定位精度,默认为 kCLLocationAccuracyBest

@property (nonatomic, assign) CLLocationAccuracy desiredAccuracy

Declared In

TencentLBSGeofenceManager.h

  distanceFilter

定位的最小更新距离,默认为 kCLDistanceFilterNone

@property (nonatomic, assign) CLLocationDistance distanceFilter

Declared In

TencentLBSGeofenceManager.h

  detectionInterval

检测频率间隔,默认为 0 秒(每次位置更新都检测)。 用于控制围栏检测的频率,降低性能消耗。

@property (nonatomic, assign) NSTimeInterval detectionInterval

Discussion

检测频率间隔,默认为 0 秒(每次位置更新都检测)。 用于控制围栏检测的频率,降低性能消耗。

Declared In

TencentLBSGeofenceManager.h

  adaptiveDistanceFilterEnabled

是否启用自适应距离过滤器(智能频率控制),默认为 YES。

@property (nonatomic, assign, getter=isAdaptiveDistanceFilterEnabled) BOOL adaptiveDistanceFilterEnabled

Discussion

是否启用自适应距离过滤器(智能频率控制),默认为 YES。

开启时,SDK 会根据当前位置到最近围栏边界的距离,自动调整定位频率。 距离围栏越近定位频率越高,距离越远频率越低,以平衡检测精度与功耗。

关闭时,使用 distanceFilter 属性设置的固定值。

Warning: 开启自适应模式后,手动设置的 distanceFilter 值将不会生效,直到关闭自适应模式。

Declared In

TencentLBSGeofenceManager.h

  currentDistanceFilter

当前实际生效的距离过滤值(只读)。自适应模式下会动态变化,固定模式下等于 distanceFilter。

@property (nonatomic, readonly) CLLocationDistance currentDistanceFilter

Declared In

TencentLBSGeofenceManager.h

  coordinateType

围栏使用的坐标系类型,默认为 TencentLBSLocationCoordinateTypeGCJ02。 用于指定传入的围栏坐标和回调的定位坐标所使用的坐标系。

@property (nonatomic, assign) TencentLBSLocationCoordinateType coordinateType

Discussion

围栏使用的坐标系类型,默认为 TencentLBSLocationCoordinateTypeGCJ02。 用于指定传入的围栏坐标和回调的定位坐标所使用的坐标系。

  • TencentLBSLocationCoordinateTypeGCJ02: 火星坐标(国测局坐标),与腾讯地图一致。 系统定位获取的 WGS-84 坐标会自动转换为 GCJ-02 后再与围栏坐标进行比较。 在中国大陆地区会进行坐标转换,海外地区不进行转换。
  • TencentLBSLocationCoordinateTypeWGS84: 地球坐标,直接使用系统定位的原始坐标。

Warning: 需要在添加围栏之前设置,且围栏坐标系需与此设置保持一致,否则会导致围栏判断偏差。

Declared In

TencentLBSGeofenceManager.h

– addCircleGeofenceWithCenter:radius:identifier:

添加圆形地理围栏

- (void)addCircleGeofenceWithCenter:(CLLocationCoordinate2D)center radius:(CLLocationDistance)radius identifier:(NSString *)identifier

Parameters

center

围栏中心坐标,坐标系需与 coordinateType 设置一致

radius

围栏半径,单位为米

identifier

围栏的唯一标识符

Discussion

添加圆形地理围栏

Note: 建议单个 Manager 实例的围栏数量不超过 1000 个,过多的围栏会增加位置检测的计算开销

Declared In

TencentLBSGeofenceManager.h

– addCircleGeofenceWithCenter:radius:identifier:userInfo:

添加圆形地理围栏(附带自定义数据)

- (void)addCircleGeofenceWithCenter:(CLLocationCoordinate2D)center radius:(CLLocationDistance)radius identifier:(NSString *)identifier userInfo:(nullable NSDictionary<NSString*,id<NSSecureCoding> > *)userInfo

Parameters

center

围栏中心坐标

radius

围栏半径,单位为米

identifier

围栏的唯一标识符

userInfo

自定义附加信息

Discussion

添加圆形地理围栏(附带自定义数据)

Declared In

TencentLBSGeofenceManager.h

– addPolygonGeofenceWithCoordinates:identifier:

添加多边形地理围栏

- (void)addPolygonGeofenceWithCoordinates:(NSArray<NSValue*> *)coordinates identifier:(NSString *)identifier

Parameters

coordinates

多边形顶点坐标数组,元素为 NSValue 包装的 CLLocationCoordinate2D,最少需要 3 个顶点

identifier

围栏的唯一标识符

Discussion

添加多边形地理围栏

Declared In

TencentLBSGeofenceManager.h

– addPolygonGeofenceWithCoordinates:identifier:userInfo:

添加多边形地理围栏(附带自定义数据)

- (void)addPolygonGeofenceWithCoordinates:(NSArray<NSValue*> *)coordinates identifier:(NSString *)identifier userInfo:(nullable NSDictionary<NSString*,id<NSSecureCoding> > *)userInfo

Parameters

coordinates

多边形顶点坐标数组,元素为 NSValue 包装的 CLLocationCoordinate2D,最少需要 3 个顶点

identifier

围栏的唯一标识符

userInfo

自定义附加信息

Discussion

添加多边形地理围栏(附带自定义数据)

Declared In

TencentLBSGeofenceManager.h

– addPolygonGeofenceWithCCoordinates:count:identifier:

添加多边形地理围栏(C 数组方式)

- (void)addPolygonGeofenceWithCCoordinates:(const CLLocationCoordinate2D *)coordinates count:(NSUInteger)count identifier:(NSString *)identifier

Parameters

coordinates

多边形顶点坐标 C 数组

count

顶点数量,最少需要 3 个

identifier

围栏的唯一标识符

Discussion

添加多边形地理围栏(C 数组方式)

Declared In

TencentLBSGeofenceManager.h

– addGeofenceRegion:

添加已创建的围栏区域对象

- (void)addGeofenceRegion:(TencentLBSGeofenceRegion *)region

Parameters

region

围栏区域对象,支持 TencentLBSGeofenceCircleRegion、TencentLBSGeofencePolygonRegionTencentLBSGeofenceDistrictRegion

Discussion

添加已创建的围栏区域对象

Declared In

TencentLBSGeofenceManager.h

– addDistrictGeofenceWithKeyword:identifier:

添加行政区划围栏(关键字方式)。 keyword 支持两种形式: - 纯数字(如 @“110108"):视为行政区划代码(adcode),直接查询边界多边形数据 - 中文名称(如 @"朝阳区"):先搜索匹配的行政区划,再查询边界多边形数据

- (void)addDistrictGeofenceWithKeyword:(NSString *)keyword identifier:(NSString *)identifier

Parameters

keyword

行政区划关键字,可以是 adcode(如 @“110108")或中文名称(如 @"海淀区")

identifier

围栏的基础标识符。当匹配到多个行政区划时,实际标识符会追加 “_序号” 后缀

Discussion

添加行政区划围栏(关键字方式)。 keyword 支持两种形式: - 纯数字(如 @“110108"):视为行政区划代码(adcode),直接查询边界多边形数据 - 中文名称(如 @"朝阳区"):先搜索匹配的行政区划,再查询边界多边形数据

如果使用中文名称且存在多个同名行政区划(如"朝阳区"同时匹配北京市朝阳区和长春市朝阳区), 会为每个匹配结果都创建围栏,identifier 自动追加 “序号” 后缀(如 “district朝阳区0"、"district朝阳区_1")。 每个围栏创建完成(成功或失败)都会单独回调 tencentLBSGeofenceManager:didCreateDistrictRegion:forKeyword:error: 。

Warning: 此方法为异步操作,围栏不会立即添加。请在代理回调中确认围栏创建是否成功。

Declared In

TencentLBSGeofenceManager.h

– addDistrictGeofenceWithKeyword:identifier:userInfo:

添加行政区划围栏(关键字方式,附带自定义数据)

- (void)addDistrictGeofenceWithKeyword:(NSString *)keyword identifier:(NSString *)identifier userInfo:(nullable NSDictionary<NSString*,id<NSSecureCoding> > *)userInfo

Parameters

keyword

行政区划关键字,可以是 adcode 或中文名称

identifier

围栏的基础标识符

userInfo

自定义附加信息

Discussion

添加行政区划围栏(关键字方式,附带自定义数据)

Declared In

TencentLBSGeofenceManager.h

– removeGeofenceWithIdentifier:

根据标识符移除指定围栏

- (void)removeGeofenceWithIdentifier:(NSString *)identifier

Parameters

identifier

要移除的围栏标识符

Discussion

根据标识符移除指定围栏

Declared In

TencentLBSGeofenceManager.h

– removeGeofenceRegion:

移除指定围栏区域对象

- (void)removeGeofenceRegion:(TencentLBSGeofenceRegion *)region

Parameters

region

要移除的围栏区域对象

Discussion

移除指定围栏区域对象

Declared In

TencentLBSGeofenceManager.h

– removeAllGeofences

移除所有地理围栏

- (void)removeAllGeofences

Declared In

TencentLBSGeofenceManager.h

– geofenceRegionWithIdentifier:

根据标识符获取围栏区域

- (nullable TencentLBSGeofenceRegion *)geofenceRegionWithIdentifier:(NSString *)identifier

Parameters

identifier

围栏标识符

Return Value

对应的围栏区域对象,未找到时返回 nil

Discussion

根据标识符获取围栏区域

Declared In

TencentLBSGeofenceManager.h

– geofenceRegionsContainingCoordinate:

获取指定坐标所在的围栏列表

- (NSArray<TencentLBSGeofenceRegion*> *)geofenceRegionsContainingCoordinate:(CLLocationCoordinate2D)coordinate

Parameters

coordinate

待查询的坐标

Return Value

包含该坐标的围栏区域数组

Discussion

获取指定坐标所在的围栏列表

Declared In

TencentLBSGeofenceManager.h

– isCoordinate:insideGeofenceWithIdentifier:

主动查询指定坐标相对于指定围栏的状态

- (BOOL)isCoordinate:(CLLocationCoordinate2D)coordinate insideGeofenceWithIdentifier:(NSString *)identifier

Parameters

coordinate

待查询的坐标

identifier

围栏标识符

Return Value

坐标在围栏内返回 YES,否则返回 NO

Discussion

主动查询指定坐标相对于指定围栏的状态

Declared In

TencentLBSGeofenceManager.h

– startMonitoring

开始监控所有已添加的围栏

- (void)startMonitoring

Declared In

TencentLBSGeofenceManager.h

– stopMonitoring

停止监控所有围栏

- (void)stopMonitoring

Declared In

TencentLBSGeofenceManager.h

– pauseMonitoring

暂停围栏监控,保留围栏数据但暂停位置检测

- (void)pauseMonitoring

Declared In

TencentLBSGeofenceManager.h

– resumeMonitoring

恢复已暂停的围栏监控

- (void)resumeMonitoring

Declared In

TencentLBSGeofenceManager.h

  monitoring

围栏监控是否正在运行

@property (nonatomic, assign, readonly, getter=isMonitoring) BOOL monitoring

Declared In

TencentLBSGeofenceManager.h