使用高德猎鹰SDK仿滴滴打车,多辆车辆实时更新位置信息。附加服务端对应实现思路。
效果
效果图实现关键点
1、标注使用MAAnimatedAnnotation
2、标注title使用TerminalID,和“查询实时位置回调”中的AMapTrackQueryLastPointRequest *request“ terminalID”对应。
3、同时查询多个终端的实时位置 AMapTrackQueryLastPoint可调用多次。
实现步骤
开始前请先参照高德开发文档创建好工程,高德猎鹰SDK文档。或下载本文中对应的demo。
1、获取轨迹上报的参数(serviceID、TerminalID)。
获取serviceID,参照高德服务管理文档
获取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];
}
//查询终端结果
- (void)onQueryTerminalDone:(AMapTrackQueryTerminalRequest *)request response:(AMapTrackQueryTerminalResponse *)response
{
//查询成功
if ([[response terminals] count] > 0) {
//查询到结果,使用 Terminal ID
NSString *terminalID = [[[response terminals] firstObject] tid];
[self saveTerminalId:terminalID];
//启动上报服务(service id),参考下一步
}
else {
//查询结果为空,创建新的terminal
AMapTrackAddTerminalRequest *addRequest = [[AMapTrackAddTerminalRequest alloc] init];
addRequest.serviceID = self.trackManager.serviceID;
addRequest.terminalName = [self saveDeviceModel];
[self.trackManager AMapTrackAddTerminal:addRequest];
}
}
//创建终端结果
- (void)onAddTerminalDone:(AMapTrackAddTerminalRequest *)request response:(AMapTrackAddTerminalResponse *)response {
//创建terminal成功
NSString *terminalID = [response terminalID];
[self saveTerminalId:terminalID];
//启动上报服务(service id),参考下一步
}
//错误回调
- (void)didFailWithError:(NSError *)error associatedRequest:(id)request {
if ([request isKindOfClass:[AMapTrackQueryTerminalRequest class]]) {
//查询参数错误
}
if ([request isKindOfClass:[AMapTrackAddTerminalRequest class]]) {
//创建terminal失败
}
NSLog(@"%@", error);
_msgLabel.text = [NSString stringWithFormat:@"初始化失败"];
}
- (void)saveTerminalId:(NSString *)terminalId {
[[NSUserDefaults standardUserDefaults] setObject:terminalId forKey:@"terminalId"];
[[NSUserDefaults standardUserDefaults] synchronize];
_msgLabel.text = [NSString stringWithFormat:@"初始化成功:设备terminalId: %@", terminalId];
}
获取完后将terminalID保存本地,供接下来轨迹上报和查询使用。
2、开始轨迹上报
(1)、创建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];
// 配置本地缓存大小,默认最多缓存50MB数据
[self.trackManager setLocalCacheMaxSize:50];
}
(3)、开启轨迹上报
//开始服务
AMapTrackManagerServiceOption *serviceOption = [[AMapTrackManagerServiceOption alloc] init];
serviceOption.terminalID = kAMapTrackTerminalID;
[self.trackManager startServiceWithOptions:serviceOption];
//service 开启结果回调
- (void)onStartService:(AMapTrackErrorCode)errorCode {
if (errorCode == AMapTrackErrorOK) {
//开始服务成功,继续开启收集上报
[self.trackManager startGatherAndPack];
} else {
//开始服务失败
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.label.text = @"开始服务失败";
[hud hideAnimated:YES afterDelay:1];
}
}
//gather 开启结果回调
- (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、查询对应终端TerminalID的实时位置信息。
(1)、创建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)、初始化标注(地图添加终端对应的标注)。
- (void)setupAnnotation {
_annotationData = @[].mutableCopy;
#warning 设置多个时,数组中添加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)、轨迹查询回调(重点在这,查询完后需要将位置、方向信息,让MAAnimatedAnnotation addMoveAnimationWithKeyCoordinates方法调用),并在完成时继续调用AMapTrackQueryLastPoint获取终端最后位置信息。
#pragma mark 查询实时位置回调
- (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];
}
}
到这里基本功能已经实现了。
服务端对应关系
1、轨迹上报的TerminalID终端ID和服务器单个终端/或用户ID关联(用户信息中包含TerminalID)。TerminalID由高德地图SDK生成,然后传给服务器。TerminalID由terminalName终端名字和高德控制平台serviceID确定。
2、根据服务器列表中的TerminalID,同时查询多个终端的实时位置 AMapTrackQueryLastPoint。
3、轨迹上报,只通知服务器已开启轨迹上报,轨迹的位置信息保存在高德轨迹服务中。
QA:
1、怎么判断地图中实时显示哪些终端?
:由后台接口返回需要显示的终端列表(数据中包含TerminalID),接口判断依据应是当前定位“范围”加“终端是否在实时上报轨迹信息”。
2、怎么判断终端是否在实时上报轨迹信息?
:由自己业务逻辑判断何时开启/关闭。
本文中demo链接:https://github.com/weiweilong/MAPLocationGithub 谢谢阅读!
网友评论