This commit is contained in:
DDIsFriend
2023-08-18 17:28:57 +08:00
commit f0e8a1709d
4282 changed files with 192396 additions and 0 deletions
@@ -0,0 +1,92 @@
//
// DDMATrackManager.h
// DDMAMapKit_Private
// Created by DDIsFriend on 2023/3/5.
#import <Foundation/Foundation.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapTrackKit/AMapTrackKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface DDMATrackManager : NSObject
+ (instancetype)shareManager;
+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;
+ (void)startWithAppKey:(nullable NSString *)appKey;
@property (nonatomic, copy)NSString *serviceID; // 初始化猎鹰服务前必须设置服务id
@property (nonatomic, copy)NSString *fullAccuracyPurpose; // 初始化猎鹰服务前必须设置
@property (nonatomic, copy)NSString *terminalID; // 启动服务前必须设置设备id
@property (nonatomic, copy)NSString *terminalName; // 这应该是一个唯一的名字,利用它来获取terminalID
@property (nonatomic, copy,nullable)NSString *trackID; // 当轨迹id存在时,上报服务回保存在trackID中,否则将以散点形式上报
@property (nonatomic, assign)BOOL isServing; // 是否已开启服务
@property (nonatomic, assign)BOOL isGathering; // 是否已开启采集
// 初始化猎鹰服务
- (void)initTraceService;
/**
* @brief 设定定位信息的采集周期和上传周期,注意:上传周期必须为采集周期的整数倍
* @param gatherTimeInterval 定位信息的采集周期,单位秒,有效值范围[1, 60]
* @param packTimeInterval 定位信息的上传周期,单位秒,有效值范围[5, 3000]
*/
- (void)changeGatherAndPackTimeInterval:(NSInteger)gatherTimeInterval packTimeInterval:(NSInteger)packTimeInterval;
/**
* @brief 设定允许的本地缓存最大值
* @param cacheMaxSize 本地缓存最大值,单位MB,默认+∞,有效值范围[50,+∞)。
* @return 是否设定成功
*/
- (BOOL)setLocalCacheMaxSize:(NSInteger)cacheMaxSize;
//查询终端是否存在
/// @warning 开始上报前先创建terminal,可以拿到terminalID
- (void)queryTerminal:(NSString *)terminalName;
// 创建轨迹
/// @warning 开始上报到指定轨迹前先创建track,可以拿到trackID
- (void)addTrackID;
/// 启动猎鹰服务,不建议直接开启服务,应该使'- (void)queryTerminal:(NSString *)terminalName'方法来开启服务
/// @warning serviceID,terminalID必不可少,而trackID是指定轨迹时才需要
- (void)startService;
// 关闭猎鹰服务
- (void)stopService;
// 开启采集服务
- (void)startGatherAndPack;
// 关闭采集服务
- (void)stopGatherAndPack;
// 创建轨迹成功的回调
@property (nonatomic, copy,nullable)void(^addTrackOnDone)(AMapTrackAddTrackRequest *request,AMapTrackAddTrackResponse *response);
// 开始服务的回调
@property (nonatomic, copy,nullable)void(^startServiceOnDone)(AMapTrackErrorCode error);
// 关闭服务的回调
@property (nonatomic, copy,nullable)void(^stopServiceOnDone)(AMapTrackErrorCode error);
// 开始采集的回调
@property (nonatomic, copy,nullable)void(^startGatherAndPackOnDone)(AMapTrackErrorCode error);
// 关闭采集的回调
@property (nonatomic, copy,nullable)void(^stopGatherAndPackOnDone)(AMapTrackErrorCode error);
// query失败回调
@property (nonatomic, copy,nullable)void(^queryFailWithErrorOnDone)(NSError *error,id request);
// 查询设备位置,queryLastPointOnDone回调
- (void)queryLastPointWith:(nullable NSString *)trackID;
@property (nonatomic, copy,nullable)void(^queryLastPointOnDone)(AMapTrackQueryLastPointRequest *request,AMapTrackQueryLastPointResponse *response);
// 查询设备一段时间内的行驶里程,queryTrackDistanceOnDone回调
- (void)queryTrackDistanceWith:(long long)startTime endTime:(long long)endTime trackID:(nullable NSString *)trackID;
@property (nonatomic, copy,nullable)void(^queryTrackDistanceOnDone)(AMapTrackQueryTrackDistanceRequest *request,AMapTrackQueryTrackDistanceResponse *response);
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,315 @@
//
// DDMATrackManager.m
// DDMAMapKit_Private
// Created by DDIsFriend on 2023/3/5.
#import "DDMATrackManager.h"
#import <DDLogKit_Private/DDOCLog.h>
@interface DDMATrackManager ()<AMapTrackManagerDelegate>
@property (nonatomic, strong)AMapTrackManager *trackManager;
@end
@implementation DDMATrackManager
// MARK: <Class Initialize>
+ (void)startWithAppKey:(nullable NSString *)appKey{
[[AMapServices sharedServices] setEnableHTTPS:YES];
[AMapServices sharedServices].apiKey = appKey;
}
+ (instancetype)shareManager{
static DDMATrackManager *_Nullable _manager = nil;
static dispatch_once_t token;
dispatch_once(&token, ^{
_manager = [[self alloc] initWithSuper];
});
return _manager;
}
- (instancetype)initWithSuper{
self = [super init];
if (self == nil) return nil;
return self;
}
- (instancetype)init{
@throw [[NSException alloc] initWithName:NSGenericException reason:@"'init' is unavailable,please use 'shareManager' instead." userInfo:nil];
}
// MARK: <Start Service>
- (void)initTraceService{
NSAssert(self.serviceID != nil, @"猎鹰serviceID = nil");
NSAssert(self.fullAccuracyPurpose != nil, @"请设置fullAccuracyPurpose的值");
if (self.serviceID == nil) {
return;
}
if (self.fullAccuracyPurpose == nil) {
return;
}
AMapTrackManagerOptions *option = [[AMapTrackManagerOptions alloc] init];
option.serviceID = self.serviceID; //Service ID 需要根据需要进行修改
//初始化AMapTrackManager
self.trackManager = [[AMapTrackManager alloc] initWithOptions:option];
self.trackManager.delegate = self;
[self.trackManager setPausesLocationUpdatesAutomatically:NO];
}
//查询终端是否存在
- (void)queryTerminal:(NSString *)terminalName{
AMapTrackQueryTerminalRequest *request = [[AMapTrackQueryTerminalRequest alloc] init];
request.serviceID = self.serviceID;
request.terminalName = terminalName;
[self.trackManager AMapTrackQueryTerminal:request];
}
//查询终端结果
- (void)onQueryTerminalDone:(AMapTrackQueryTerminalRequest *)request response:(AMapTrackQueryTerminalResponse *)response
{
//查询成功
if ([[response terminals] count] > 0) {
//查询到结果,使用 Terminal ID
NSString *terminalID = [[[response terminals] firstObject] tid];
DDLog(@"--------------------------------LBS查询终端成功,terminalID = %@--------------------------------\n--------------------------------准备启动服务--------------------------------",terminalID);
//启动上报服务(service id),参考下一步
self.terminalID = terminalID;
[self startService];
}
else {
DDLog(@"--------------------------------LBS查询终端为空--------------------------------\n--------------------------------准备创建终端--------------------------------");
//查询结果为空,创建新的terminal
[self addTerminal:request.terminalName];
}
}
// 创建终端
- (void)addTerminal:(NSString *)terminalName{
AMapTrackAddTerminalRequest *addRequest = [[AMapTrackAddTerminalRequest alloc] init];
addRequest.serviceID = self.trackManager.serviceID;
addRequest.terminalName = terminalName;
[self.trackManager AMapTrackAddTerminal:addRequest];
}
//创建终端结果
- (void)onAddTerminalDone:(AMapTrackAddTerminalRequest *)request response:(AMapTrackAddTerminalResponse *)response {
DDLog(@"--------------------------------LBS创建终端成功--------------------------------\n--------------------------------准备开启服务--------------------------------");
//创建terminal成功
NSString *terminalID = [response terminalID];
//启动上报服务(service id),参考下一步
self.terminalID = terminalID;
[self startService];
}
//错误回调
- (void)didFailWithError:(NSError *)error associatedRequest:(id)request {
if ([request isKindOfClass:[AMapTrackQueryTerminalRequest class]]) {
//查询参数错误
DDLog(@"--------------------------------LBS查询终端失败--------------------------------");
}
if ([request isKindOfClass:[AMapTrackAddTerminalRequest class]]) {
//创建terminal失败
DDLog(@"--------------------------------LBS创建终端失败--------------------------------");
}
if ([request isKindOfClass:[AMapTrackAddTrackRequest class]]) {
//创建轨迹失败
DDLog(@"--------------------------------LBS创建轨迹失败--------------------------------");
}
if ([request isKindOfClass:[AMapTrackQueryLastPointRequest class]]) {
//查询失败
DDLog(@"--------------------------------LBS查询最后位置失败--------------------------------");
}
if ([request isKindOfClass:[AMapTrackQueryTrackDistanceRequest class]]) {
//查询失败
DDLog(@"--------------------------------LBS查询距离失败--------------------------------");
}
if (self.queryFailWithErrorOnDone) {
self.queryFailWithErrorOnDone(error, request);
}
}
//创建轨迹
- (void)addTrackID{
AMapTrackAddTrackRequest *request = [[AMapTrackAddTrackRequest alloc] init];
request.serviceID = self.trackManager.serviceID;
request.terminalID = self.trackManager.terminalID;
[self.trackManager AMapTrackAddTrack:request];
}
- (void)onAddTrackDone:(AMapTrackAddTrackRequest *)request response:(AMapTrackAddTrackResponse *)response
{
//创建轨迹成功,开始采集
self.trackID = response.trackID;
DDLog(@"---------------------------------创建轨迹成功----------------------------------")
if (self.addTrackOnDone != nil) {
self.addTrackOnDone(request, response);
}
}
- (void)startService{
if (self.isServing == false) {
// 当开启服务时,设置允许后台定位
[self.trackManager setAllowsBackgroundLocationUpdates:YES];
AMapTrackManagerServiceOption *serviceOption = [[AMapTrackManagerServiceOption alloc] init];
serviceOption.terminalID = self.terminalID;//Terminal ID 需要根据需要进行修改
[self.trackManager startServiceWithOptions:serviceOption];
}
}
- (void)stopService{
if (self.isServing == true) {
[self.trackManager stopService];
}
}
- (void)startGatherAndPack{
if (self.trackID != nil) {
self.trackManager.trackID = self.trackID;
NSLog(@"轨迹id---------------%@",self.trackID);
DDLog(@"---------------------------------设置轨迹成功----------------------------------")
}
[self.trackManager startGatherAndPack];
}
- (void)stopGatherAndPack{
[self.trackManager stopGaterAndPack];
}
//service 开启结果回调
- (void)onStartService:(AMapTrackErrorCode)errorCode {
if (errorCode == AMapTrackErrorOK) {
DDLog("--------------------------------开启猎鹰服务成功--------------------------------\n--------------------------------准备采集--------------------------------");
// 已开启服务成功
self.isServing = YES;
} else {
//开始服务失败
DDLog("--------------------------------开启猎鹰服务失败--%ld",errorCode);
}
if (self.startServiceOnDone) {
self.startServiceOnDone(errorCode);
}
}
// 关闭猎鹰服务
- (void)onStopService:(AMapTrackErrorCode)errorCode {
if (errorCode == AMapTrackErrorOK) {
//关闭猎鹰服务成功
self.isServing = false;
DDLog(@"--------------------------------关闭猎鹰服务成功--------------------------------");
} else {
//关闭猎鹰服务失败
DDLog(@"--------------------------------关闭猎鹰服务失败--------------------------------");
}
if (self.stopServiceOnDone) {
self.stopServiceOnDone(errorCode);
}
}
//gather 开启结果回调
- (void)onStartGatherAndPack:(AMapTrackErrorCode)errorCode {
if (errorCode == AMapTrackErrorOK) {
//开始采集成功
self.isGathering = YES;
DDLog(@"--------------------------------开始猎鹰采集成功--------------------------------");
} else {
//开始采集失败
DDLog(@"--------------------------------开始猎鹰采集失败--------------------------------");
}
if (self.startGatherAndPackOnDone) {
self.startGatherAndPackOnDone(errorCode);
}
}
// 关闭采集
- (void)onStopGatherAndPack:(AMapTrackErrorCode)errorCode {
if (errorCode == AMapTrackErrorOK) {
//关闭采集成功
self.isGathering = NO;
DDLog(@"--------------------------------关闭猎鹰采集成功--------------------------------");
} else {
//关闭采集失败
DDLog(@"--------------------------------关闭猎鹰采集失败--------------------------------");
}
if (self.stopGatherAndPackOnDone) {
self.stopGatherAndPackOnDone(errorCode);
}
}
// 查询最后的位置
- (void)queryLastPointWith:(nullable NSString *)trackID{
AMapTrackQueryLastPointRequest *request = [[AMapTrackQueryLastPointRequest alloc] init];
request.serviceID = self.trackManager.serviceID;
request.terminalID = self.trackManager.terminalID;
if (trackID != nil) {
request.trackID = trackID;
}
[self.trackManager AMapTrackQueryLastPoint:request];
}
// 查询最后的位置结果
- (void)onQueryLastPointDone:(AMapTrackQueryLastPointRequest *)request response:(AMapTrackQueryLastPointResponse *)response
{
if (self.queryLastPointOnDone != nil) {
self.queryLastPointOnDone(request,response);
}
}
- (void)queryTrackDistanceWith:(long long)startTime endTime:(long long)endTime trackID:(nullable NSString *)trackID{
AMapTrackQueryTrackDistanceRequest *request = [[AMapTrackQueryTrackDistanceRequest alloc] init];
request.serviceID = self.trackManager.serviceID;
request.terminalID = self.trackManager.terminalID;
request.startTime = startTime;
request.endTime = endTime;
request.correctionMode = @"driving";
if (trackID != nil) {
request.trackID = self.trackManager.trackID;
}
DDLog(@"--------------------------------当前猎鹰查询distance:\n起始时间:%lld\n结束时间:%lld--------------------------------",startTime,endTime);
[self.trackManager AMapTrackQueryTrackDistance:request];
}
- (void)onQueryTrackDistanceDone:(AMapTrackQueryTrackDistanceRequest *)request response:(AMapTrackQueryTrackDistanceResponse *)response
{
//查询成功
NSLog(@"--------------------------------onQueryTrackDistanceDone%@--------------------------------", response.formattedDescription);
if (self.queryTrackDistanceOnDone != nil) {
DDLog(@"--------------------------------当前猎鹰查询distance:%lu--------------------------------",(unsigned long)response.distance);
self.queryTrackDistanceOnDone(request, response);
}
}
- (void)changeGatherAndPackTimeInterval:(NSInteger)gatherTimeInterval packTimeInterval:(NSInteger)packTimeInterval{
[self.trackManager changeGatherAndPackTimeInterval:gatherTimeInterval packTimeInterval:packTimeInterval];
}
- (BOOL)setLocalCacheMaxSize:(NSInteger)cacheMaxSize{
return [self.trackManager setLocalCacheMaxSize:50];
}
// 获取精准定位
- (void)amapTrackManager:(nonnull AMapTrackManager *)manager doRequireTemporaryFullAccuracyAuth:(nonnull CLLocationManager *)locationManager completion:(nonnull void (^)(NSError * _Nonnull))completion API_AVAILABLE(ios(14.0)){
[locationManager requestTemporaryFullAccuracyAuthorizationWithPurposeKey:self.fullAccuracyPurpose completion:^(NSError *error){
if(completion){
completion(error);
}
}];
}
@end