百度SDK周边雷达使用流程

作者: JanzenChen | 来源:发表于2016-10-19 19:01 被阅读1064次

    Demo线上库: https://github.com/Xcodemonkey/RunPathDemo.git


    效果图:


    雷达周边.PNG
    实时路径.PNG
    ------ 一 :配置 ------

    申请AK --> 注册雷达功能 --> 下载SDK --> 导入SDK及相关依赖库及基础资源(mapapi.bundle)(可使用pod导入免去到依赖库的麻烦,但是SDK相对较大,耗时长,且我们可能只需其中的一些功能.) --> 配置info.plist文件(定位授权/后台等),也可以代码申请授权

    至此完毕,另外在此郑重提出:
    一: Xcode7 与 Xcode8 配置授权的key是有差别的,尝试过在Xcode7上用Xcode8的key来配置发现是无效的
    二:百度SDK使用了C++混编,可按官网配置编译方式,也可改变任意一个文件后缀为.mm.

    Xcode8的key:
        <key>Privacy - Location Always Usage Description</key>
        <string>只有开启定位功能才能正常使用百度导航</string>
        <key>Privacy - Location When In Use Usage Description</key>
        <string>只有开启定位功能才能正常使用百度导航</string>
    
    Xcode7的key:
        <key>NSLocationAlwaysUsageDescription</key>
        <true/>
        <key>NSLocationWhenInUseUsageDescription</key>
        <true/>
    

    ------ 二 :申请百度地图授权 ------

    在app启动完成的回调中创建地图管理者(BMKMapManager)并申请地图授权

       - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        // 要使用百度地图,请先启动BaiduMapManager
        self.mapManager = [[BMKMapManager alloc]init];
        // 如果要关注网络及授权验证事件,请设定 generalDelegate参数
        BOOL ret = [_mapManager start:BMKKey  generalDelegate:nil];
        if (!ret) {
            NSLog(@"manager start failed!");
        }
        
        self.window = [[UIWindow alloc] initWithFrame:SCREEN_BOUNDS];
        BaseTabBarController *rootVc = [[BaseTabBarController alloc] init];
        self.window.rootViewController = rootVc;
        [self.window makeKeyAndVisible];
        
    //    //由于IOS8中定位的授权机制改变 需要进行手动授权
    //    CLLocationManager  *locationManager = [[CLLocationManager alloc] init];
    //    //获取授权认证
    //    [locationManager requestAlwaysAuthorization];
    //    [locationManager requestWhenInUseAuthorization];
        
        return YES;
    }
    

    ------ 三 :设置mapView及locationService属性 ------

    在map view 和 locationService的懒加载中设置需要的相关属性,最重要的是delegate,其他不多说.想看的自己下我的Demo吧,里面注释非常详细.线上库地址在文章开头.


    ------ 四 :设置必要时的释放------
    - (void)viewWillAppear:(BOOL)animated
    {
        [_mapView viewWillAppear];
        _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
        [self.radarManager addRadarManagerDelegate:self]; // 添加radar delegate
    }
    - (void)viewWillDisappear:(BOOL)animated
    {
        [_mapView viewWillDisappear];
        _mapView.delegate = nil; // 不用时,置nil
        [_radarManager removeRadarManagerDelegate:self];//不用需移除,否则影响内存释放
    }
    - (void)dealloc {
        _mapView.delegate = nil; // 不用时,置nil
        [BMKRadarManager releaseRadarManagerInstance];
    }
    

    ------ 五 :遵守协议并实现代理方法 ------
    #pragma mark - **** BMKMapViewDelegate ****
    /** 地图加载完成 */
    - (void)mapViewDidFinishLoading:(BMKMapView *)mapView {
        // 开启定位服务
        [self.locService startUserLocationService];
    }
    
    /** 大头针样式 */
    - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        newAnnotationView.pinColor = BMKPinAnnotationColorPurple; // 颜色
        newAnnotationView.annotation = annotation;
            newAnnotationView.image = [[UIImage imageNamed:@"person"] imageWithBoderWidth:1.0 boderColor:COLOR_WHITE];
    //        [UIImage imageNamed:@"person"];   //把大头针换成别的图片
        } 
        newAnnotationView.bounds = CGRectMake(0, 0, 20, 20);
    //    newAnnotationView.calloutOffset = CGPointMake(20, 20);
        return newAnnotationView;
    }
    
    #pragma mark - **** BMKLocationServiceDelegate ****
    /** 处理位置坐标更新 */
    - (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
        // 更新当前位置信息
        [self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
       [self.mapView updateLocationData:userLocation];
      self.currentLocation = userLocation.location;
    }
    
    /** 雷达搜索按钮点击事件 */
    - (void)OtherCarNearBy:(UIButton *)sender {
        sender.enabled = NO;
        DBLog(@"OtherCarNearBy");
        [MBProgressHUD showLoadingWithMsg:@"检索周边用户" inView:self.view];
        [self clearAction]; // 清除先前的提交的位置
        // 百度默认上传间隔最小为5秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            sender.enabled = YES;
        });
    }
    /** 清除我的位置信息 */
    - (void)clearAction{
        BOOL res = [self.radarManager clearMyInfoRequest];// 清除位置信息
        if (YES == res) {
            DBLog(@"clear 成功");
        } else {
            [MBProgressHUD hideFromView:self.view];
            [MBProgressHUD showMsg:@"清除先前位置失败"];
            DBLog(@"clear 失败");
        }
    }
    
    #pragma mark - **** BMKRadarManagerDelegate ****
    /** 调用清除位置信息API后的回调 */
    - (void)onGetRadarClearMyInfoResult:(BMKRadarErrorCode) error {
        if (BMK_RADAR_NO_ERROR == error || BMK_RADAR_USERID_NOT_EXIST == error) {
             DBLog(@"成功清除我的位置");
    
       // 上传当前自己的位置信息
            BOOL sendRes = [self.radarManager uploadInfoRequest:[self getCurrInfo]];
            if (sendRes) {
                DBLog(@"upload 成功");
            } else {
                [MBProgressHUD hideFromView:self.view];
                [MBProgressHUD showMsg:@"上传位置失败,请5秒后再试"];
                DBLog(@"upload 失败");
            }
        }
    }
    /** 获取当前的信息(自定义方法) */
    - (BMKRadarUploadInfo *)getCurrInfo {
        BMKRadarUploadInfo *userInfo = [[BMKRadarUploadInfo alloc]init];
        [self.lock lock]; // 上锁
        userInfo.pt = self.currentLocation.coordinate;
    //    userInfo.extInfo = kUUID;// 一般以设备号为标注
        userInfo.extInfo = [NSString stringWithFormat:@"游客 %4d",arc4random_uniform(9999)];
        [self.lock unlock]; // 解锁
        DBLog(@"%f - %f", userInfo.pt.latitude, userInfo.pt.longitude);
        return userInfo;
    }
    
    /** 调用上传位置信息API后的回调 */
    - (void)onGetRadarUploadResult:(BMKRadarErrorCode) error {
        if (error == BMK_RADAR_NO_ERROR) {
            DBLog(@"成功上传我的位置");
            // 设置雷达搜索条件
            BMKRadarNearbySearchOption *option = [[BMKRadarNearbySearchOption alloc] init];
            option.radius = 10000; // 搜索半径 10km
            option.sortType = BMK_RADAR_SORT_TYPE_DISTANCE_FROM_NEAR_TO_FAR; // 排序方式
            option.centerPt = self.currentLocation.coordinate; // 搜索中心
            option.pageIndex = 0;
            option.pageCapacity = 30;
            //    NSDate *eDate = [NSDate date];
            //    //    eDate = [NSDate dateWithTimeInterval:-3600 * 3 sinceDate:eDate];
            //    NSDate *date = [NSDate dateWithTimeInterval:-3600 * 4 sinceDate:eDate];
            //    BMKDateRange *dateRange = [[BMKDateRange alloc] init];
            //    dateRange.startDate = date;
            //    dateRange.endDate = eDate;
            //    DBLog(@"%@ ,  %@", date, eDate);
            //    option.dateRange = dateRange;
            
            // 查询所上传的位置的周边用户
            BOOL getRes = [self.radarManager getRadarNearbySearchRequest:option];
            if (getRes) {
                [MBProgressHUD hideFromView:self.view];
                DBLog(@"get 成功");
            } else {
                [MBProgressHUD hideFromView:self.view];
                [MBProgressHUD showMsg:@"获取周边失败"];
                DBLog(@"get 失败");
            }
        }
    }
    
    /** 调用查询API后的回调, result中infoList就是周围同款或多款(申请雷达服务时可设置多个AK)APP的用户 */
    - (void)onGetRadarNearbySearchResult:(BMKRadarNearbyResult*) result error:(BMKRadarErrorCode) error {
        if (BMK_RADAR_NO_RESULT == error) {
            [MBProgressHUD showMsg:@"周边无其他用户"];
        }
        [self.mapView removeAnnotations:self.curnearByCars]; // 移除先前的周围用户
        NSArray *nearByCars = result.infoList;
        self.curnearByCars = nearByCars; // 记录当前周围用户
        for(BMKRadarNearbyInfo* info in nearByCars){
          // 添加用户大头针
            [self addAnnotationWithLongitude:info.pt.longitude latitude:info.pt.latitude title:info.extInfo];
        }
    }
    

    简单的流程就是这样啦,有兴趣的可以看看,只要多台手机安装,就可以搜索到周边有其他人啦.

    code: @XiYue on https://github.com

    相关文章

      网友评论

      • xieyinghao:你好,编辑通过,跑不了,怎么处理。。。。谢谢
        dyld: Library not loaded: @rpath/MBProgressHUD.framework/MBProgressHUD
        Referenced from: /var/containers/Bundle/Application/074B8710-0BA2-4388-9010-9ACFB30B6A8D/RunPathDemo.app/RunPathDemo
        Reason: no suitable image found. Did find:
        /private/var/containers/Bundle/Application/074B8710-0BA2-4388-9010-9ACFB30B6A8D/RunPathDemo.app/Frameworks/MBProgressHUD.framework/MBProgressHUD: code signing blocked mmap() of '/private/var/containers/Bundle/Application/074B8710-0BA2-4388-9010-9ACFB30B6A8D/RunPathDemo.app/Frameworks/MBProgressHUD.framework/MBProgressHUD'
        /private/var/containers/Bundle/Application/074B8710-0BA2-4388-9010-9ACFB30B6A8D/RunPathDemo.app/Frameworks/MBProgressHUD.framework/MBProgressHUD: code signing blocked mmap() of '/private/var/containers/Bundle/Application/074B8710-0BA2-4388-9010-9ACFB30B6A8D/RunPathDemo.app/Frameworks/MBProgressHUD.framework/MBProgressHUD'
        /private/var/containers/Bundle/Application/074B8710-0BA2-4388-9010-9ACFB30B6A8D/RunPathDemo.app/Frameworks/MBProgressHUD.framework/MBProgressHUD: code signing blocked mmap() of '/private/var/containers/Bundle/Application/074B8710-0BA2-4388-9010-9ACFB30B6A8D/RunPathDemo.app/Frameworks/MBProgressHUD.framework/MBProgressHUD'
      • xieyinghao:你好,编辑通过,跑不了怎么办。谢谢
        xieyinghao:@熙玥 我可以再请教你一个问题吗: 百度地图点击大头针事件,怎么判断点击了那个大头针
        xieyinghao:@熙玥 谢谢
        JanzenChen:@xieyinghao 更新下第三方约束,或者你直接不要引用MBP…HUD了。

      本文标题:百度SDK周边雷达使用流程

      本文链接:https://www.haomeiwen.com/subject/yebxuttx.html