美文网首页
百度地图的集成 ---定位

百度地图的集成 ---定位

作者: 这个姑凉儿 | 来源:发表于2018-02-27 10:38 被阅读0次

    前言:如何定位到自己的位置,并且在地图上标记自己的位置,方法如下

    打开百度开放平台

    • 创建应用,把图所圈的根据自己的项目填入

    打开xcode创建一个新的工程

    • 开始集成到工程 按开发文档进行集成有两种方式一种是cocoapods,一种是普通的集成,如果是oc就按oc的环境配置,swift就按swift环境配置,在此为了方便我采用cocoapods集成


    • 如图所示已成功下载了百度的包,然后打开工程编译一下看是否报错,如果编译成功,恭喜你以成功把百度的包集成到了自己的工程中,下面就要加入必要代码,以及一些权限


    • ViewController内的代码
    #import <BaiduMapAPI_Map/BMKMapComponent.h>
    #import <BaiduMapAPI_Location/BMKLocationComponent.h>
    #import <BaiduMapAPI_Search/BMKGeocodeSearch.h>
    @interface ViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>{
        BMKLocationService* _locService;
        BMKMapView* _mapView;
        BMKGeoCodeSearch *_geoCodeSearch;
    }
    @end
    @implementation ViewController
    -(void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        _locService.delegate = self;
        _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
    }
    -(void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        _locService.delegate = nil;
        _mapView.delegate = nil; // 不用时,置nil
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // 设置地图定位
        [self setupBMKLocation];
        
    }
    - (void)setupBMKLocation {
        _mapView = [[BMKMapView alloc]init];
        _mapView.frame = self.view.bounds;
        [self.view addSubview:_mapView];
        //初始化BMKLocationService
        _locService = [[BMKLocationService alloc]init];
        _locService.delegate = self;
        // 初始化编码服务
        _geoCodeSearch = [[BMKGeoCodeSearch alloc] init];
        _geoCodeSearch.delegate = self;
        //启动LocationService
        [_locService startUserLocationService];
        _mapView.showsUserLocation = YES;//显示定位图层
        _mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态为普通定位模式
    }
    #pragma mark - BMKLocationServiceDelegate 实现相关delegate 处理位置信息更新
    
    /**
     *在地图View将要启动定位时,会调用此函数
     *@param mapView 地图View
     */
    - (void)willStartLocatingUser
    {
        NSLog(@"start locate");
    }
    
    /**
     *用户方向更新后,会调用此函数
     *@param userLocation 新的用户位置
     */
    - (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
    {
        NSLog(@"heading is %@",userLocation.heading);
    }
    
    /**
     *用户位置更新后,会调用此函数
     *@param userLocation 新的用户位置
     */
    - (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
    {
        
        BMKCoordinateRegion region;
        region.center.latitude = userLocation.location.coordinate.latitude;
        region.center.longitude = userLocation.location.coordinate.longitude;
        region.span.latitudeDelta = 0.2;
        region.span.longitudeDelta = 0.2;
        if (_mapView)
        {
            _mapView.region = region;
        }
        [_mapView setZoomLevel:19.0];
        [_locService stopUserLocationService];//定位完成停止位置更新
        
        //添加当前位置的标注
        CLLocationCoordinate2D coord;
        coord.latitude = userLocation.location.coordinate.latitude;
        coord.longitude = userLocation.location.coordinate.longitude;
        
        BMKPointAnnotation *_pointAnnotation = [[BMKPointAnnotation alloc] init];
        _pointAnnotation.coordinate = coord;
        
        //反地理编码出地理位置
        CLLocationCoordinate2D pt=(CLLocationCoordinate2D){0,0};
        pt=(CLLocationCoordinate2D){coord.latitude,coord.longitude};
        BMKReverseGeoCodeOption *reverseGeoCodeOption = [[BMKReverseGeoCodeOption alloc] init];
        reverseGeoCodeOption.reverseGeoPoint = pt;
        //发送反编码请求.并返回是否成功
        BOOL flag = [_geoCodeSearch reverseGeoCode:reverseGeoCodeOption];
        
        if (flag) {
            NSLog(@"反geo检索发送成功");
        } else {
            NSLog(@"反geo检索发送失败");
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [_mapView removeOverlays:_mapView.overlays];
            [_mapView setCenterCoordinate:coord animated:true];
            [_mapView addAnnotation:_pointAnnotation];
            
        });
        
    }
    /**
     *在地图View停止定位后,会调用此函数
     *@param mapView 地图View
     */
    - (void)didStopLocatingUser
    {
        NSLog(@"stop locate");
    }
    /**
     *定位失败后,会调用此函数
     *@param mapView 地图View
     *@param error 错误号,参考CLError.h中定义的错误号
     */
    - (void)didFailToLocateUserWithError:(NSError *)error
    {
        NSLog(@"location error");
        //    NSString *city = [[NSUserDefaults standardUserDefaults] objectForKey:@"cityNmae"];
        //    [self.cityBtn setTitle:city forState:UIControlStateNormal];
    }
    
    // 反地理编码
    - (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {
        
        if (error == 0) {
            
            NSString *cityName = [result.poiList.firstObject city];
            
            
            NSLog(@"%@, %@", [result.poiList.firstObject city], result.address);
            
            
            
            //        // 定位的city
            //        [[NSUserDefaults standardUserDefaults] setObject:[result.poiList.firstObject city] forKey:@"city"];
            //        [[NSUserDefaults standardUserDefaults] synchronize];
            //
            //        // 保存定位的街道地址
            //        [[NSUserDefaults standardUserDefaults] setObject:result.addressDetail.streetName forKey:@"street"];
            //        [[NSUserDefaults standardUserDefaults] synchronize];
            
        }
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    @end
    
    • 这样仅仅是把地图显示出来了,和我们想要的还不一样,发现控制台打印出我们没加权限,现在加一下


    • 在真机上运行发现,可以定位到自己的位置,也在地图上有一个大头针标记在自己的位置上,但是我们的控制台打印了一些东西


      • 要求我们必须添加bundle display name ,并且我们的反检索没有成功,没有在控制台输出我们的位置,下面就按提示添加一下bundle display name ,运行一下看看效果


    • 发现加入bundle display name成功的解决了反检索的问题,并且成功的输出了我们当前的位置😊,到此就结束了定位自己的位置,并且在地图上标记出来,后续会更新关于地图的别的功能,尽请期待.....
      demo地址
      有没有帮到你呢?😁
      (欢迎大家对不合适的地方进行指正,看完觉得有帮到你给点个赞👍吧)

    相关文章

      网友评论

          本文标题:百度地图的集成 ---定位

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