ios 百度鷹眼整合

weixin_34194087發表於2017-02-22

百度鷹眼整合遇到的坑
下面說一下整合步驟很全
1,使用cocoapods匯入百度地圖的基礎的SDK: pod ‘BaiduMapKit’

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


2460743-c3d66e7e0a5ea63b.png
如圖.png

3,設定標頭檔案路徑(選擇剛剛匯入的SDK,Show in Finder,選擇工程->Build Settings ,搜尋框輸入search,找到Header Serach Paths ,雙擊這行的右邊,彈出一個大的輸入框,把剛才Show in Finder的資料夾裡面的Headers資料夾直接拖到大輸入框裡)


2460743-3d870668604c20a7.png
如圖.png

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

5…. 所謂開啟後臺位置定位


2460743-adb68b196dcce26d.png
如圖.png

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


2460743-231acae20f926af9.png
如圖.png

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


2460743-8a2056ba700a1f13.png
如圖.png

12,在工程的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,在需要用到的控制器裡


2460743-849608c664d03149.png
如圖.png
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];
    NSNumber *status = [dic objectForKey:@"status"];
    if (0 == [status longValue]) {
        NSArray *entities = [dic objectForKey:@"entities"];
        NSDictionary *entity = [entities objectAtIndex:0];
        NSDictionary *realtimePoint = [entity objectForKey:@"realtime_point"];
        NSArray *location = [realtimePoint objectForKey:@"location"];
        longitudeOfEntity = [[location objectAtIndex:0] doubleValue];
        latitudeOfEntity = [[location objectAtIndex:1] doubleValue];
        dispatch_async(dispatch_get_main_queue(), ^{
            [_mapView removeOverlays:_mapView.overlays];
            [_mapView removeAnnotations:_mapView.annotations];
        });
        [self addPointAnnotation];
    }
}


//新增當前位置的標註
-(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,和百度開發者中心的安全碼要保持一致,否則會出現只有白色網格的情況。

  1. “230 image not found” Build Options->Enabled Content Contains Swift Code(Xcode 8 的是Always Embed Swift Standard Libraries)->YES
  2. “指定track不存在” 去到百度開發者官網的service_id那裡,點選相應的ID的專案,建立一個entityName,或者選擇某個entityName(如果有的話)去替換你工程裡的 entityName

如果跑步起來看這裡看這裡


2460743-bece81b3cb92a121.png
1.png
2460743-247f47e60ecd3e91.png
1.png

相關文章