iOS集成百度鹰眼时的坎坷里程
随着智能出行技术的普及,获取用户定位的需求在很多App中都很常见,似乎是用户的一举一动都在我们App后台的掌握之中。掌握用户的定位信息,也是很多后续App更好发展的前提条件,于是乎很多***(keng)钻(die)刻(chan)薄(pin)的需求便应运而生:包括实时获取用户定位、实时计算一定时间内的行驶距离、实时描绘车辆的行驶轨迹、监控车辆的形式速度、行驶习惯...(不说了,累死程序🐶不偿命,先让我哭会)。针对产品的需求,幸运的是这次提前告知,让我有充足的时间准备,这次就简单研究了一下百度API中的百度鹰眼服务,自我感觉挺吊的,简单集成了一下,包括获取实时位置信息,规定时间内的行驶距离,一定时间内的行驶轨迹点;话不多说,直接上干货。
1、集成前的准备工作,百度鹰眼官网的文档信息已经够详细了,我就不再班门弄斧了,地址:http://lbsyun.baidu.com/index.php?title=ios-yingyan/guide/createservice
2、百度鹰眼的工程配置文档也是较为详细,一步步来感觉不会出现什么大问题:http://lbsyun.baidu.com/index.php?title=ios-yingyan/guide/buildproject
3、针对百度鹰眼的SDK,我简单封装了一个工具类(菜鸟,无吐槽),直接上代码吧,相应的方法都有相应的注释:
BaiduTranceHelpTool.h文件
#import <Foundation/Foundation.h> typedef void(^EntityUserMsgBlock)(NSDictionary *dictMsg);//符合条件的entity信息及其实时位置Block typedef void(^QueryDistanceBlock)(NSString *distanceStr);//查询指定时间段内的里程 typedef void(^TranceHistoryBlock)(id historyDict);//查询历史轨迹点 @interface BaiduTranceHelpTool : NSObject @property (nonatomic, strong) EntityUserMsgBlock entityUserMsgBlock; @property (nonatomic, strong) QueryDistanceBlock queryDistanceBlock; @property (nonatomic, strong) TranceHistoryBlock tranceHistoryBlock; + (instancetype)shareInstance; /** * 开始记录轨迹 */ - (void)stratTranceWithEntityNames:(NSString *)entityNames; /** * 查询所有符合条件的entity信息及其实时位置 * * @param entityNames 用户名(查询条件) * @param entityUserMsgBlock 回传信息 */ - (void)queryEntityUsermsgWithEntityNames:(NSString *)entityNames andMsgBlock:(void(^)(NSDictionary *dictMsg))entityUserMsgBlock; /** * 查询指定时间段内的里程(单位:米) * * @param entityNames 用户名(查询条件) * @param startTimeStr 起始时间 * @param endTimeStr 结束时间 * @param queryDistanceBlock 返回里程(单位:米) */ - (void)queryDistanceWithEntityNames:(NSString *)entityNames wirhStartTime:(NSString *)startTimeStr endTime:(NSString *)endTimeStr withDistanceBlock:(void(^)(NSString *distanceStr))queryDistanceBlock; /** * 重载的历史轨迹查询方法,可以指定轨迹纠偏的选项、里程补偿方式、结果排序方式 * * @param entityNames 用户名(查询条件) * @param startTimeStr 起始时间 * @param endTimeStr 结束时间 * @param tranceHistoryBlock 返回历史轨迹点 */ - (void)historyTranceWithEntityNames:(NSString *)entityNames wirhStartTime:(NSString *)startTimeStr endTime:(NSString *)endTimeStr withTranceHistoryBlock:(void(^)(id historyDict))tranceHistoryBlock; /** * 结束百度鹰眼定位轨迹记录 */ - (void)stopbaiduTrance; @end
BaiduTranceHelpTool.m文件
#import "BaiduTranceHelpTool.h" #import <BaiduTraceSDK/BaiduTraceSDK.h> #import <BaiduTraceSDK/BaiduTraceSDK-Swift.h> #define BAIDUAK @"百度上申请AK" #define BAIDUCODE @"开通鹰眼服务时绑定的bundleId" #define BAIDUSERVICEID 鹰眼服务的serviceid @interface BaiduTranceHelpTool ()<ApplicationEntityDelegate,ApplicationTrackDelegate,ApplicationServiceDelegate> @property (nonatomic, strong) BTRACE *traceInstance; @end @implementation BaiduTranceHelpTool +(instancetype)shareInstance{ static BaiduTranceHelpTool * __dataHelper; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ __dataHelper = [[BaiduTranceHelpTool alloc] init]; }); return __dataHelper; } - (void)stratTranceWithEntityNames:(NSString *)entityNames{ if (!_traceInstance) { _traceInstance = [[BTRACE alloc] initWithAk:BAIDUAK mcode:BAIDUCODE serviceId:BAIDUSERVICEID entityName:entityNames operationMode:2]; } [_traceInstance setInterval:20 packInterval:40]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [[BTRACEAction shared] startTrace:self trace:_traceInstance]; }); [[BTRACEAction shared] changeGatherAndPackIntervalsAfterStartTrace:self gatherInterval:5 packInterval:30]; } - (void)queryEntityUsermsgWithEntityNames:(NSString *)entityNames andMsgBlock:(void (^)(NSDictionary *))entityUserMsgBlock{ self.entityUserMsgBlock = entityUserMsgBlock; [[BTRACEAction shared] queryEntityList:self serviceId:129170 entityNames:entityNames columnKey:nil activeTime:0 returnType:0 pageSize:0 pageIndex:0]; } #pragma mark - 返回所有符合条件的entity信息及其实时位置 - (void)onQueryEntityList:(NSData * _Nonnull)data{ NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"%@",dict); __weak typeof(self)Wself = self; dispatch_async(dispatch_get_main_queue(), ^{ if (Wself.entityUserMsgBlock) { Wself.entityUserMsgBlock(dict); } }); } - (void)queryDistanceWithEntityNames:(NSString *)entityNames wirhStartTime:(NSString *)startTimeStr endTime:(NSString *)endTimeStr withDistanceBlock:(void (^)(NSString *))queryDistanceBlock{ self.queryDistanceBlock = queryDistanceBlock; [[BTRACEAction shared] queryDistance:self serviceId:129170 entityName:entityNames isProcessed:1 processOption:@"need_denoise=1,need_vacuate=1,need_mapmatch=1" supplementMode:@"driving" startTime:[self getUNIXTimeFromTimeStr:startTimeStr] endTime:[self getUNIXTimeFromTimeStr:endTimeStr]]; } #pragma mark - 查询指定时间段内的里程(单位:米) - (void)onQueryDistance:(NSData * _Nonnull)data{ NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"%@",dict); __weak typeof(self)Wself = self; dispatch_async(dispatch_get_main_queue(), ^{ if (Wself.queryDistanceBlock) { Wself.queryDistanceBlock([NSString stringWithFormat:@"%@",dict[@"distance"]]); } }); } - (void)historyTranceWithEntityNames:(NSString *)entityNames wirhStartTime:(NSString *)startTimeStr endTime:(NSString *)endTimeStr withTranceHistoryBlock:(void (^)(id))tranceHistoryBlock{ self.tranceHistoryBlock = tranceHistoryBlock; [[BTRACEAction shared] getTrackHistory:self serviceId:129170 entityName:entityNames startTime:[self getUNIXTimeFromTimeStr:startTimeStr] endTime:[self getUNIXTimeFromTimeStr:endTimeStr] simpleReturn:0 isProcessed:1 processOption:@"need_denoise=1,need_vacuate=1,need_mapmatch=1" supplementMode:@"driving" sortType:1 pageSize:0 pageIndex:0]; } #pragma mark - 重载的历史轨迹查询方法,可以指定轨迹纠偏的选项、里程补偿方式、结果排序方式 - (void)onGetHistoryTrack:(NSData *)data{ id dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"%@",dict); __weak typeof(self)Wself = self; dispatch_async(dispatch_get_main_queue(), ^{ if (Wself.tranceHistoryBlock) { Wself.tranceHistoryBlock(dict); } }); } #pragma mark - 字符转时间戳 - (int)getUNIXTimeFromTimeStr:(NSString *)timeStr{ NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSInteger interval = [zone secondsFromGMTForDate:[self getDateFromStr:timeStr]]; NSDate *localeDate = [[self getDateFromStr:timeStr] dateByAddingTimeInterval:interval]; NSString *time = [NSString stringWithFormat:@"%ld",(long)[localeDate timeIntervalSince1970]]; NSLog(@"date = %@",time); return [time integerValue]; } #pragma mark - 字符转daten - (NSDate *)getDateFromStr:(NSString *)dateStr{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *date = [dateFormatter dateFromString:dateStr]; NSLog(@"date = %@",date); return date; } #pragma mark - 结束轨迹记录 - (void)stopbaiduTrance{ [[BTRACEAction shared] stopTrace:self trace:_traceInstance]; }
在需要使用鹰眼服务的时候直接调用相应的方法就行了.