使用高德獵鷹SDK仿滴滴打車瓢捉,多輛車輛實時更新位置信息。附加服務(wù)端對應(yīng)實現(xiàn)思路椎镣。
效果
實現(xiàn)關(guān)鍵點
1状答、標(biāo)注使用MAAnimatedAnnotation
2惊科、標(biāo)注title使用TerminalID亮钦,和“查詢實時位置回調(diào)”中的AMapTrackQueryLastPointRequest *request“ terminalID”對應(yīng)。
3蜡娶、同時查詢多個終端的實時位置 AMapTrackQueryLastPoint可調(diào)用多次映穗。
實現(xiàn)步驟
開始前請先參照高德開發(fā)文檔創(chuàng)建好工程,高德獵鷹SDK文檔宿接∧丽或下載本文中對應(yīng)的demo走诞。
1、獲取軌跡上報的參數(shù)(serviceID肮塞、TerminalID)。
獲取serviceID枕赵,參照高德服務(wù)管理文檔
獲取TerminalID如下:
- (void)setupTrack {
AMapTrackManagerOptions *option = [[AMapTrackManagerOptions alloc] init];
option.serviceID = kAMapTrackServiceID;
//初始化AMapTrackManager
self.trackManager = [[AMapTrackManager alloc] initWithOptions:option];
self.trackManager.delegate = self;
//查詢終端是否存在
AMapTrackQueryTerminalRequest *request = [[AMapTrackQueryTerminalRequest alloc] init];
request.serviceID = self.trackManager.serviceID;
request.terminalName = [self saveDeviceModel];
[self.trackManager AMapTrackQueryTerminal:request];
}
//查詢終端結(jié)果
- (void)onQueryTerminalDone:(AMapTrackQueryTerminalRequest *)request response:(AMapTrackQueryTerminalResponse *)response
{
//查詢成功
if ([[response terminals] count] > 0) {
//查詢到結(jié)果拷窜,使用 Terminal ID
NSString *terminalID = [[[response terminals] firstObject] tid];
[self saveTerminalId:terminalID];
//啟動上報服務(wù)(service id)篮昧,參考下一步
}
else {
//查詢結(jié)果為空,創(chuàng)建新的terminal
AMapTrackAddTerminalRequest *addRequest = [[AMapTrackAddTerminalRequest alloc] init];
addRequest.serviceID = self.trackManager.serviceID;
addRequest.terminalName = [self saveDeviceModel];
[self.trackManager AMapTrackAddTerminal:addRequest];
}
}
//創(chuàng)建終端結(jié)果
- (void)onAddTerminalDone:(AMapTrackAddTerminalRequest *)request response:(AMapTrackAddTerminalResponse *)response {
//創(chuàng)建terminal成功
NSString *terminalID = [response terminalID];
[self saveTerminalId:terminalID];
//啟動上報服務(wù)(service id)窄潭,參考下一步
}
//錯誤回調(diào)
- (void)didFailWithError:(NSError *)error associatedRequest:(id)request {
if ([request isKindOfClass:[AMapTrackQueryTerminalRequest class]]) {
//查詢參數(shù)錯誤
}
if ([request isKindOfClass:[AMapTrackAddTerminalRequest class]]) {
//創(chuàng)建terminal失敗
}
NSLog(@"%@", error);
_msgLabel.text = [NSString stringWithFormat:@"初始化失敗"];
}
- (void)saveTerminalId:(NSString *)terminalId {
[[NSUserDefaults standardUserDefaults] setObject:terminalId forKey:@"terminalId"];
[[NSUserDefaults standardUserDefaults] synchronize];
_msgLabel.text = [NSString stringWithFormat:@"初始化成功:設(shè)備terminalId: %@", terminalId];
}
獲取完后將terminalID保存本地嫉你,供接下來軌跡上報和查詢使用躏惋。
2、開始軌跡上報
(1)距误、創(chuàng)建mapview准潭。具體代碼參照高德地圖文檔或demo中代碼域仇。
(2)、初始化軌跡上報
- (void)setupReport {
AMapTrackManagerOptions *option = [[AMapTrackManagerOptions alloc] init];
option.serviceID = kAMapTrackServiceID;
//初始化AMapTrackManager
self.trackManager = [[AMapTrackManager alloc] initWithOptions:option];
self.trackManager.delegate = self;
// 配置獵鷹SDK
[self.trackManager setAllowsBackgroundLocationUpdates:YES];
[self.trackManager setPausesLocationUpdatesAutomatically:NO];
/**
定位信息的采集周期,單位秒沽讹,有效值范圍[1, 60]爽雄。
定位信息的上傳周期,單位秒叹谁,有效值范圍[5, 3000]
*/
[self.trackManager changeGatherAndPackTimeInterval:2 packTimeInterval:2];
// 配置本地緩存大小,默認(rèn)最多緩存50MB數(shù)據(jù)
[self.trackManager setLocalCacheMaxSize:50];
}
(3)、開啟軌跡上報
//開始服務(wù)
AMapTrackManagerServiceOption *serviceOption = [[AMapTrackManagerServiceOption alloc] init];
serviceOption.terminalID = kAMapTrackTerminalID;
[self.trackManager startServiceWithOptions:serviceOption];
//service 開啟結(jié)果回調(diào)
- (void)onStartService:(AMapTrackErrorCode)errorCode {
if (errorCode == AMapTrackErrorOK) {
//開始服務(wù)成功憔涉,繼續(xù)開啟收集上報
[self.trackManager startGatherAndPack];
} else {
//開始服務(wù)失敗
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.label.text = @"開始服務(wù)失敗";
[hud hideAnimated:YES afterDelay:1];
}
}
//gather 開啟結(jié)果回調(diào)
- (void)onStartGatherAndPack:(AMapTrackErrorCode)errorCode {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
if (errorCode == AMapTrackErrorOK) {
//開始采集成功
hud.label.text = @"開始采集成功";
} else {
//開始采集失敗
hud.label.text = @"開始采集失敗";
}
[hud hideAnimated:YES afterDelay:1];
}
3析苫、查詢對應(yīng)終端TerminalID的實時位置信息。
(1)国旷、創(chuàng)建mapview跪但。具體代碼參照高德地圖文檔或demo中代碼峦萎。
(2)、初始化軌跡查詢
- (void)setupReport {
AMapTrackManagerOptions *option = [[AMapTrackManagerOptions alloc] init];
option.serviceID = kAMapTrackServiceID;
//初始化AMapTrackManager
self.trackManager = [[AMapTrackManager alloc] initWithOptions:option];
self.trackManager.delegate = self;
AMapTrackQueryLastPointRequest *request = [[AMapTrackQueryLastPointRequest alloc] init];
request.serviceID = self.trackManager.serviceID;
request.terminalID = kAMapTrackTerminalID;
// 糾偏
// request.correctionMode = @"denoise=1,mapmatch=1,threshold=0,mode=driving";
[self.trackManager AMapTrackQueryLastPoint:request];
}
(3)涂身、初始化標(biāo)注(地圖添加終端對應(yīng)的標(biāo)注)搓蚪。
- (void)setupAnnotation {
_annotationData = @[].mutableCopy;
#warning 設(shè)置多個時,數(shù)組中添加TerminalID
NSArray *annotations = @[kAMapTrackTerminalID];
[annotations enumerateObjectsUsingBlock:^(NSString *annotationTitle, NSUInteger idx, BOOL * _Nonnull stop) {
MAAnimatedAnnotation *anno = [[MAAnimatedAnnotation alloc] init];
anno.title = annotationTitle;
[_annotationData addObject:anno];
}];
[self.mapView addAnnotations:_annotationData];
}
(4)悴能、軌跡查詢回調(diào)(重點在這雳灾,查詢完后需要將位置、方向信息炒嘲,讓MAAnimatedAnnotation addMoveAnimationWithKeyCoordinates方法調(diào)用)匈庭,并在完成時繼續(xù)調(diào)用AMapTrackQueryLastPoint獲取終端最后位置信息。
#pragma mark 查詢實時位置回調(diào)
- (void)onQueryLastPointDone:(AMapTrackQueryLastPointRequest *)request response:(AMapTrackQueryLastPointResponse *)response {
//查詢成功
NSLog(@"onQueryLastPointDone%@", response.formattedDescription);
if(!_isLoction){
self.mapView.centerCoordinate = response.lastPoint.coordinate;
_isLoction = YES;
}
AMapTrackPoint *lastPoint = response.lastPoint;
CLLocationCoordinate2D *coordinate = malloc(sizeof(CLLocationCoordinate2D));
CLLocationCoordinate2D *point = &coordinate[0];
point->latitude = lastPoint.coordinate.latitude;
point->longitude = lastPoint.coordinate.longitude;
__block MAAnimatedAnnotation *anno;
[_annotationData enumerateObjectsUsingBlock:^(MAAnimatedAnnotation *annotation, NSUInteger idx, BOOL * _Nonnull stop) {
if([annotation.title isEqualToString:request.terminalID]){
anno = annotation;
}
}];
anno.movingDirection = lastPoint.direction;
[anno addMoveAnimationWithKeyCoordinates:coordinate count:1 withDuration:1 withName:anno.title completeCallback:^(BOOL isFinished) {
if(isFinished){
[self.trackManager AMapTrackQueryLastPoint:request];
}
}];
}
- (void)didFailWithError:(NSError *)error associatedRequest:(id)request {
if ([request isKindOfClass:[AMapTrackQueryLastPointRequest class]]) {
//查詢失敗
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.label.text = @"查詢失敗";
[hud hideAnimated:YES afterDelay:1];
}
}
到這里基本功能已經(jīng)實現(xiàn)了夭拌。
服務(wù)端對應(yīng)關(guān)系
1鸽扁、軌跡上報的TerminalID終端ID和服務(wù)器單個終端/或用戶ID關(guān)聯(lián)(用戶信息中包含TerminalID)。TerminalID由高德地圖SDK生成躲雅,然后傳給服務(wù)器巩那。TerminalID由terminalName終端名字和高德控制平臺serviceID確定。
2噪生、根據(jù)服務(wù)器列表中的TerminalID东囚,同時查詢多個終端的實時位置 AMapTrackQueryLastPoint。
3页藻、軌跡上報,只通知服務(wù)器已開啟軌跡上報璃吧,軌跡的位置信息保存在高德軌跡服務(wù)中废境。
QA:
1、怎么判斷地圖中實時顯示哪些終端巴元?
:由后臺接口返回需要顯示的終端列表(數(shù)據(jù)中包含TerminalID),接口判斷依據(jù)應(yīng)是當(dāng)前定位“范圍”加“終端是否在實時上報軌跡信息”逮刨。
2修己、怎么判斷終端是否在實時上報軌跡信息迎罗?
:由自己業(yè)務(wù)邏輯判斷何時開啟/關(guān)閉。
本文中demo鏈接:https://github.com/weiweilong/MAPLocationGithub 謝謝閱讀!