本文主要是介绍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];}
}
这篇关于iOS开发:高德地理围栏SDK-小白教学的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!