iOS開發(第三方使用)——百度地圖鷹眼軌跡SDK接入

>=3.0版本的接入(適配XCode8.3以上)

  1. 登錄百度地圖開放平臺,找到iOS的鷹眼軌跡的SDK,下載,然后把BaiduTraceSDK.framework導入工程(選擇工程->General ,把SDK拖到Embedded Baniaries)


  1. 在buidsettings輸入bite,選擇Enable bite code,值為NO;

  2. 在plist添加NSLocationAlwaysUsageDescription

  3. 。。。


  1. 使用
    —1—
#import "BaiduTraceSDK/BaiduTraceSDK.h"

添加代理BTKTraceDelegate, BTKFenceDelegate, BTKTrackDelegate, BTKEntityDelegate

- (void)viewDidLoad {
    [super viewDidLoad];
    // 使用SDK的任何功能前,都需要先調用initInfo:方法設置基礎信息。
    BTKServiceOption *sop = [[BTKServiceOption alloc] initWithAK:AK mcode:mcode serviceID:serviceID keepAlive:false];
    [[BTKAction sharedInstance] initInfo:sop];
}

—2—開啟服務

BTKStartServiceOption *op = [[BTKStartServiceOption alloc] initWithEntityName:entityName];
    [[BTKAction sharedInstance] startService:op delegate:self];

服務回調
-(void)onStartService:(BTKServiceErrorCode)error {
    NSLog(@"start service response: %lu", (unsigned long)error);//0成功
}

—3—收集軌跡

[[BTKAction sharedInstance] startGather:self];

回調
-(void)onStartGather:(BTKGatherErrorCode)error {
    NSLog(@"start gather response: %lu", (unsigned long)error);//0成功,正在收集軌跡
}

—4—結束

[[BTKAction sharedInstance] stopGather:self];

回調
-(void)onStopGather:(BTKGatherErrorCode)error {
    NSLog(@"stop gather response: %lu", (unsigned long)error);//6成功結束
}

—5—查詢軌跡

NSUInteger endTime = [[NSDate date] timeIntervalSince1970];
    BTKQueryTrackProcessOption *option = [[BTKQueryTrackProcessOption alloc] init];
    option.denoise = FALSE;
    option.vacuate = FALSE;//抽稀屬性只有查詢歷史軌跡時才有作用
    option.mapMatch = FALSE;
    option.radiusThreshold = 55;

    BTKQueryHistoryTrackRequest *request = [[BTKQueryHistoryTrackRequest alloc] initWithEntityName:entityName startTime:endTime - 84400 endTime:endTime isProcessed:TRUE processOption:option supplementMode:BTK_TRACK_PROCESS_OPTION_SUPPLEMENT_MODE_DRIVING outputCoordType:BTK_COORDTYPE_BD09LL sortType:BTK_TRACK_SORT_TYPE_ASC pageIndex:1 pageSize:100 serviceID:serviceID tag:13];
    [[BTKTrackAction sharedInstance] queryHistoryTrackWith:request delegate:self];

回調
-(void)onQueryHistoryTrack:(NSData *)response {
//軌跡數據
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"track history response: %@", dict);

//數據處理

}

//顯示軌跡的回調

-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay {
    if ([overlay isKindOfClass:[BMKPolyline class]]) {
        BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
        polylineView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];
        polylineView.lineWidth = 5.0;
        return polylineView;
    }
    return nil;
}

??.0版本以下的接入(XCode8.2以下)

  1. 使用cocoapods導入百度地圖的基礎的SDK: pod ‘BaiduMapKit’

  2. 登錄百度地圖開放平臺,找到iOS的鷹眼軌跡的SDK,下載,然后把BaiduTraceSDK.framework導入工程(選擇工程->General ,把SDK拖到Embedded Baniaries)


  1. 設置頭文件路徑(選擇剛剛導入的SDK,Show in Finder,選擇工程->Build Settings ,搜索框輸入search,找到Header Serach Paths ,雙擊這行的右邊,彈出一個大的輸入框,把剛才Show in Finder的文件夾里面的Headers文件夾直接拖到大輸入框里)


  2. 導入類庫CoreLocation.framework,QuartzCore.framework,OpenGLES.framework,
    SystemConfiguration.framework,CoreGraphics.framework,
    Security.framework,libsqlite3.0.tbd,CoreTelephony.framework,libstdc++.6.0.9.tbd


  1. 解決 230 image not found 的問題(有時候工程無緣無故地在還沒有進入的時候就崩了,很可能也是這里的問題,有一次我明明之前已經設置好了,沒動過它,它也會自動地變成了NO,坑了好久)注意:Xcode 8 把這項改了名字:Always Embed Swift Standard Libraries

7. 添加Bundle display name,并且在使用到百度SDK的文件中,把文件.m后綴改為.mm
8. 允許https(在plist添加NSAppTransportSecurity,類型Dictionary ,在此目錄下添加NSAllowsArbitraryLoads,類型boolean,值為YES;)
9. 在buidsettings輸入bite,選擇Enable bite code,值為NO;
10. 在plist添加NSLocationAlwaysUsageDescription
11. 在工程的AppDelegate.h


  1. 在工程的AppDelegate.m
 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _mapManager = [[BMKMapManager alloc]init];
    BOOL ret = [_mapManager start:你在百度開放平臺創建的AK  generalDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
    return YES;
}

  (void)onGetNetworkState:(int)iError
{
    if (0 == iError) {
        NSLog(@"聯網成功");
    }
    else{
        NSLog(@"onGetNetworkState %d",iError);
    }

}

  (void)onGetPermissionState:(int)iError
{
    if (0 == iError) {
        NSLog(@"授權成功");
    }
    else {
        NSLog(@"onGetPermissionState %d",iError);
    }
}

13.在需要用到的控制器里


static NSString * entityName;
static BTRACE * traceInstance = NULL;
double latitudeOfEntity;
double longitudeOfEntity;

- (void)viewDidLoad {
    [super viewDidLoad];

_mapView=[[BMKMapView alloc] initWithFrame:self.view.frame];
    _mapView.backgroundColor=[UIColor redColor];
    [_mapView setZoomLevel:19];
    pointAnnotation = nil;

    [self.view addSubview:_mapView];

    [self doWork];
}

-(void) doWork {
    //把設備的uuid作為entityName
    entityName = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

    traceInstance = [[BTRACE alloc] initWithAk:AK mcode:MCODE serviceId:serviceID entityName: entityName operationMode: 2];
    _mapView.delegate = self; // 此處記得不用的時候需要置nil,否則影響內存的釋放
    _mapView.mapType = BMKMapTypeStandard;

    //視圖加載之后就請求實時位置
    [self queryEntityList];

}

//請求實時位置
- (void)queryEntityList {
    [[BTRACEAction shared] queryEntityList:self serviceId:serviceID entityNames:entityName columnKey:nil activeTime:0 returnType:0 pageSize:0 pageIndex:0];
}

#pragma mark - Entity相關的回調方法

- (void)onQueryEntityList:(NSData *)data {
    NSString *entityListResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"實時位置查詢結果: %@", entityListResult);
    //軌跡數據
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:[entityListResult dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
    //數據處理
}

//添加當前位置的標注
-(void)addPointAnnotation {
    CLLocationCoordinate2D coord;
    coord.latitude = latitudeOfEntity;
    coord.longitude = longitudeOfEntity;
    if (nil == pointAnnotation) {
        pointAnnotation = [[BMKPointAnnotation alloc] init];
    }
    pointAnnotation.coordinate = coord;

    CLLocationCoordinate2D pt=(CLLocationCoordinate2D){0,0};
    pt=(CLLocationCoordinate2D){latitudeOfEntity,longitudeOfEntity};
    pointAnnotation.title = @"最新位置";
    dispatch_async(dispatch_get_main_queue(), ^{
        [_mapView setCenterCoordinate:coord animated:true];
        [_mapView addAnnotation:pointAnnotation];
    });
}

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if (annotation == pointAnnotation) {
        NSString *AnnotationViewID = @"renameMark";
        BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
        if (annotationView == nil) {
            annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
            // 設置顏色
            annotationView.pinColor = BMKPinAnnotationColorPurple;
            // 從天上掉下效果
            annotationView.animatesDrop = YES;
            // 設置可拖拽
            annotationView.draggable = YES;
        }
        return annotationView;
    }
    return nil;
}

注意:1.bundle id ,工程里的mode,和百度開發者中心的安全碼要保持一致,否則會出現只有白色網格的情況。
2. “230 image not found” Build Settings->Build Options->Always Embed Swift Standard Libraries->YES
3. “指定track不存在” 去到百度開發者官網的service_id那里,點擊相應的ID的項目,創建一個entityName,或者選擇某個entityName(如果有的話)去替換你工程里的 entityName
4. 更多未知問題請去官網論壇查看,有時候可能是因為官網未能及時更新,所以看看官網公告

收錄:https://blog.csdn.net/liumude123/article/details/51660192

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容