iOS开发:高德地理围栏SDK-小白教学

功能:检测当前设备是否在规定的范围内。

详细步骤移步–>高德·地理围栏·文档

安装定位SDK

# Podfile文件配置依赖
pod 'AMapLocation-NO-IDFA'
# 终端下载依赖
pod install

配置info.plist

请添加图片描述

iOS14及以上版本使用地理围栏功能,需要在plist中配置NSLocationTemporaryUsageDescriptionDictionary字典描述,且添加自定义Key描述地理围栏的使用场景。

	<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
	<string>App会根据您的位置提供更精确的服务</string>
	<key>NSLocationTemporaryUsageDescriptionDictionary</key>
	<dict>
		<key>GeoFence</key>
		<string>App需要获取准确的位置,以便验证您是否在受支持的地区</string>
		<key>Location</key>
		<string>App需要获取准确的位置,以提供更精确的服务</string>
	</dict>
	<key>NSLocationWhenInUseUsageDescription</key>
	<string>App会根据您的位置提供更精确的服务</string>

需要获取临时高精度权限时,配置不同的key,可以展示给用户不同的描述。
请添加图片描述

引入头文件

#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>

遵循围栏代理

@interface TKGeofenceManage ()<AMapGeoFenceManagerDelegate, CLLocationManagerDelegate>
@property (nonatomic, strong) AMapGeoFenceManager *geoFenceManager;
@property (nonatomic, strong) CLLocationManager *locationManager;
@end

申请权限

#pragma mark - 位置授权
- (void)authorizationStatusManager {
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
}

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { //iOS14 之前
    NSLog(@"wuwuFQ:AMap AuthorizationStatus %d", status);
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
            NSLog(@"wuwuFQ:AMap Location 未授权");
            [self.locationManager requestAlwaysAuthorization];
            break;
        case kCLAuthorizationStatusAuthorizedAlways: //始终允许
        case kCLAuthorizationStatusAuthorizedWhenInUse://使用时允许
            NSLog(@"wuwuFQ:AMap Location 用户授权");
            break;
        case kCLAuthorizationStatusRestricted://服务受限制
        case kCLAuthorizationStatusDenied://用户拒绝
            NSLog(@"wuwuFQ:AMap Location 拒绝授权");
            break;
            
        default:
            NSLog(@"wuwuFQ:AMap Location 未知");
            break;
    }
}

- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager API_AVAILABLE(ios(14.0)) {
    NSLog(@"wuwuFQ:AMap AuthorizationStatus %d", manager.authorizationStatus);
    switch (manager.authorizationStatus) {
        case kCLAuthorizationStatusNotDetermined:
            NSLog(@"wuwuFQ:AMap Location 未授权");
            [self.locationManager requestAlwaysAuthorization];
            break;
        case kCLAuthorizationStatusAuthorizedAlways: //始终允许
        case kCLAuthorizationStatusAuthorizedWhenInUse://使用时允许
                    {
            if (manager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy) {
                NSLog(@"wuwuFQ:AMap Location 用户授权 高精度");
            } else {
                [manager requestTemporaryFullAccuracyAuthorizationWithPurposeKey:@"GeoFence" completion:^(NSError *error){
                    NSLog(@"wuwuFQ:AMap Geofence 获取高精度授权");
                    if (manager.accuracyAuthorization == CLAccuracyAuthorizationReducedAccuracy) {
                        NSLog(@"wuwuFQ:AMap Location 用户授权 低精度 无法使用围栏");
                    }
                }];
            }
        }
            break;
        case kCLAuthorizationStatusRestricted://服务受限制
        case kCLAuthorizationStatusDenied://用户拒绝
            NSLog(@"wuwuFQ:AMap Location 拒绝授权");
            break;
            
        default:
            NSLog(@"wuwuFQ:AMap Location 未知");
            break;
    }
}

初始化

[[AMapServices sharedServices] setEnableHTTPS:YES];
[AMapServices sharedServices].apiKey = [TKSettings valueOfKey:@"你的高德apiKey"];
    
self.geoFenceManager = [[AMapGeoFenceManager alloc] init];
self.geoFenceManager.delegate = self;
//需要进行通知的行为
self.geoFenceManager.activeAction = AMapGeoFenceActiveActionInside | AMapGeoFenceActiveActionOutside;
//是否允许后台定位
self.geoFenceManager.allowsBackgroundLocationUpdates = YES;

申请临时精确定位权限

在需要高精度权限时,这段代码也是必须的,而我在之前已经判断并请求了requestTemporaryFullAccuracyAuthorizationWithPurposeKey,所以这里也是没必要的

//申请临时精确定位权限
- (void)amapLocationManager:(AMapGeoFenceManager *)manager doRequireTemporaryFullAccuracyAuth:(CLLocationManager *)locationManager completion:(void (^)(NSError *))completion API_AVAILABLE(ios(14.0)) {
    [locationManager requestTemporaryFullAccuracyAuthorizationWithPurposeKey:@"GeoFence" completion:^(NSError *error){
        if(completion){
            completion(error);
        }
    }];
}

下面这段代码是在[CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined的时候调用,写不写都行,因为我已经提前判断并申请了权限。

- (void)amapGeoFenceManager:(AMapGeoFenceManager *)manager doRequireLocationAuth:(CLLocationManager*)locationManager {
    [locationManager requestAlwaysAuthorization];
}

创建围栏

请忽略用到的数据,根据自己的业务编码。

圆形围栏

NSArray *centerCoordinate = [model.center componentsSeparatedByString:@","];
CLLocationDegrees latitude  = [centerCoordinate.firstObject doubleValue];
CLLocationDegrees longitude  = [centerCoordinate.lastObject doubleValue];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
[self.geoFenceManager addCircleRegionForMonitoringWithCenter:coordinate radius:model.radius customID:customID];

多边形围栏

NSInteger count = model.points.count;
if (count >= 3) {
    CLLocationCoordinate2D *coordinates = malloc(sizeof(CLLocationCoordinate2D) * count);
    for (int i = 0; i < count; i++) {
        NSString *coordinateString = model.points[i];
        NSArray *centerCoordinate = [coordinateString componentsSeparatedByString:@","];
        CLLocationDegrees latitude  = [centerCoordinate.firstObject doubleValue];
        CLLocationDegrees longitude  = [centerCoordinate.lastObject doubleValue];
        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
        coordinates[i] = coordinate;
    }
    [self.geoFenceManager addPolygonRegionForMonitoringWithCoordinates:coordinates count:count customID:customID];
    free(coordinates);
} else {
    NSLog(@"wuwuFQ:AMap Error 多边形围栏至少三个坐标点");
}

移除围栏

- (void)removeAllGeoFenceRegions {
    [self.geoFenceManager removeAllGeoFenceRegions];
}

围栏代理

 * @brief 添加地理围栏完成后的回调,成功与失败都会调用
 * @param manager 地理围栏管理类
 * @param regions 成功添加的一个或多个地理围栏构成的数组
 * @param customID 用户执行添加围栏函数时传入的customID
 * @param error 添加失败的错误信息
 */
- (void)amapGeoFenceManager:(AMapGeoFenceManager *)manager didAddRegionForMonitoringFinished:(NSArray <AMapGeoFenceRegion *> *)regions customID:(NSString *)customID error:(NSError *)error {
    for (AMapGeoFenceRegion *region in regions) {
        if (error) {
            NSLog(@"wuwuFQ:AMap 添加地理围栏失败 纬度:%f 经度:%f -- %@ -- %@", region.currentLocation.coordinate.latitude, region.currentLocation.coordinate.longitude, region.customID, error);
        } else {
            NSLog(@"wuwuFQ:AMap 添加地理围栏成功 纬度:%f 经度:%f -- %@ -- %ld", region.currentLocation.coordinate.latitude, region.currentLocation.coordinate.longitude, region.customID, (long)region.fenceStatus);
        }
    }
}
/**
 * @brief 地理围栏状态改变时回调,当围栏状态的值发生改变,定位失败都会调用
 * @param manager 地理围栏管理类
 * @param region 状态改变的地理围栏
 * @param customID 用户执行添加围栏函数时传入的customID
 * @param error 错误信息,如定位相关的错误
 */
- (void)amapGeoFenceManager:(AMapGeoFenceManager *)manager didGeoFencesStatusChangedForRegion:(AMapGeoFenceRegion *)region customID:(NSString *)customID error:(NSError *)error {
    NSLog(@"wuwuFQ:AMap 地理围栏状态改变 纬度:%f 经度:%f -- %@ -- %ld", region.currentLocation.coordinate.latitude, region.currentLocation.coordinate.longitude, region.customID, (long)region.fenceStatus);
  
    if (self.delegate && [self.delegate respondsToSelector:@selector(didGeoFencesStatusChangedForRegion:customID:error:)]) {
        [self.delegate didGeoFencesStatusChangedForRegion:region customID:customID error:error];
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wuwuFQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值