使用高德獵鷹SDK仿滴滴打車,多輛車輛實(shí)時(shí)更新位置信息

使用高德獵鷹SDK仿滴滴打車,多輛車輛實(shí)時(shí)更新位置信息。附加服務(wù)端對(duì)應(yīng)實(shí)現(xiàn)思路。

參考高德地圖文檔:
點(diǎn)平滑移動(dòng)
查詢終端實(shí)時(shí)位置

效果

效果圖
實(shí)現(xiàn)關(guān)鍵點(diǎn)

1、標(biāo)注使用MAAnimatedAnnotation
2、標(biāo)注title使用TerminalID,和“查詢實(shí)時(shí)位置回調(diào)”中的AMapTrackQueryLastPointRequest *request“ terminalID”對(duì)應(yīng)。
3、同時(shí)查詢多個(gè)終端的實(shí)時(shí)位置 AMapTrackQueryLastPoint可調(diào)用多次。

實(shí)現(xiàn)步驟

開始前請(qǐng)先參照高德開發(fā)文檔創(chuàng)建好工程,高德獵鷹SDK文檔。或下載本文中對(duì)應(yīng)的demo。

1、獲取軌跡上報(bào)的參數(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];
        //啟動(dòng)上報(bào)服務(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];
    //啟動(dòng)上報(bào)服務(wù)(service id),參考下一步
}

//錯(cuò)誤回調(diào)
- (void)didFailWithError:(NSError *)error associatedRequest:(id)request {
    if ([request isKindOfClass:[AMapTrackQueryTerminalRequest class]]) {
        //查詢參數(shù)錯(cuò)誤
    }
    
    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保存本地,供接下來軌跡上報(bào)和查詢使用。

2、開始軌跡上報(bào)

(1)、創(chuàng)建mapview。具體代碼參照高德地圖文檔或demo中代碼。
(2)、初始化軌跡上報(bào)

- (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)、開啟軌跡上報(bào)

//開始服務(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ù)開啟收集上報(bào)
        [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、查詢對(duì)應(yīng)終端TerminalID的實(shí)時(shí)位置信息。

(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)注(地圖添加終端對(duì)應(yīng)的標(biāo)注)。

- (void)setupAnnotation {
    _annotationData = @[].mutableCopy;
    
#warning 設(shè)置多個(gè)時(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)(重點(diǎn)在這,查詢完后需要將位置、方向信息,讓MAAnimatedAnnotation addMoveAnimationWithKeyCoordinates方法調(diào)用),并在完成時(shí)繼續(xù)調(diào)用AMapTrackQueryLastPoint獲取終端最后位置信息。
#pragma mark 查詢實(shí)時(shí)位置回調(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)實(shí)現(xiàn)了。

服務(wù)端對(duì)應(yīng)關(guān)系

1、軌跡上報(bào)的TerminalID終端ID和服務(wù)器單個(gè)終端/或用戶ID關(guān)聯(lián)(用戶信息中包含TerminalID)。TerminalID由高德地圖SDK生成,然后傳給服務(wù)器。TerminalID由terminalName終端名字和高德控制平臺(tái)serviceID確定。
2、根據(jù)服務(wù)器列表中的TerminalID,同時(shí)查詢多個(gè)終端的實(shí)時(shí)位置 AMapTrackQueryLastPoint。
3、軌跡上報(bào),只通知服務(wù)器已開啟軌跡上報(bào),軌跡的位置信息保存在高德軌跡服務(wù)中。

QA:
1、怎么判斷地圖中實(shí)時(shí)顯示哪些終端?

:由后臺(tái)接口返回需要顯示的終端列表(數(shù)據(jù)中包含TerminalID),接口判斷依據(jù)應(yīng)是當(dāng)前定位“范圍”加“終端是否在實(shí)時(shí)上報(bào)軌跡信息”。

2、怎么判斷終端是否在實(shí)時(shí)上報(bào)軌跡信息?

:由自己業(yè)務(wù)邏輯判斷何時(shí)開啟/關(guān)閉。

本文中demo鏈接:https://github.com/weiweilong/MAPLocationGithub 謝謝閱讀!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容