美文网首页
iOS项目百度地图的使用2016.6

iOS项目百度地图的使用2016.6

作者: cj2527 | 来源:发表于2016-06-15 17:51 被阅读285次

    前言

    本文是<iOS项目集成百度地图2016.5>文章的后续,个人对百度地图官方文档的总结与补充。

    1.准备工作

    1.1导入头文件

    #import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
    #import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
    #import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
    #import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云检索功能所有的头文件
    #import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
    #import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件
    #import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周边雷达功能所有的头文件
    #import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件
    

    1.2无论你使用哪种功能,都需要先启动百度地图管理工具

    #import "AppDelegate.h"
    #import <BaiduMapAPI_Map/BMKMapComponent.h>
    @interface AppDelegate ()
    
    @end
    BMKMapManager* _mapManager;
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        // 要使用百度地图,请先启动BaiduMapManager
        _mapManager = [[BMKMapManager alloc]init];
        BOOL ret = [_mapManager start:@"你在百度地图开发平台注册应用的AppKey" generalDelegate:self];
        if (!ret) {
            NSLog(@"manager start failed!");
        }
        
        [self.window addSubview:navigationController.view];
        [self.window makeKeyAndVisible];
        return YES;
    }
    /**
     *  网络检查
     *
     *  @param iError 错误码
     */
    - (void)onGetNetworkState:(int)iError
    {
        if (iError) {
            NSLog(@"%d", iError);
        } else {
            NSLog(@"网络连接成功");
        }
    }
    
    - (void)onGetPermissionState:(int)iError
    {
        if (0 == iError) {
            NSLog(@"授权成功");
        }
        else {
            NSLog(@"onGetPermissionState %d",iError);
        }   
    }
    @end
    
    

    2.定位功能

    2.1可以和地图功能分离使用,单独的定位功能使用方式如下:

    -(void)viewDidLoad { //初始化BMKLocationService 
    self.locService = [[BMKLocationService alloc]init]; 
    self.locService.delegate = self; 
    //启动LocationService 
     [self.locService startUserLocationService];
     } 
    

    //实现相关delegate 处理位置信息更新

     //处理东西南北的方向变更信息
     - (void)didUpdateUserHeading:(BMKUserLocation *)userLocation { 
    //NSLog(@"heading is %@",userLocation.heading); 
    } 
    //处理位置坐标更新 
    
    - (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation { 
       // [self.locService stopUserLocationService];停止定位
        NSLog(@"当前的坐标是: 经度%.2f,纬度%.2f",userLocation.location.coordinate.longitude,userLocation.location.coordinate.latitude);
     }
    

    2.2搭配地图一起使用

    -(void)viewDidLoad {
    
    BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        mapView.delegate=self;
        [self.view addSubview:mapView];
        self.mapView = mapView;
    
     //初始化BMKLocationService 
    self.locService = [[BMKLocationService alloc]init]; 
    self.locService.delegate = self; 
    //启动LocationService 
     [self.locService startUserLocationService];
     } 
    

    //实现相关delegate 处理位置信息更新

     //处理东西南北的方向变更信息
     - (void)didUpdateUserHeading:(BMKUserLocation *)userLocation { 
    //NSLog(@"heading is %@",userLocation.heading); 
    } 
    //处理位置坐标更新 
    
    - (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation { 
       // [self.locService stopUserLocationService];停止定位
        NSLog(@"当前的坐标是: 经度%.2f,纬度%.2f",userLocation.location.coordinate.longitude,userLocation.location.coordinate.latitude);
     }
    

    3.检索功能

    百度地图SDK提供的检索服务包括以下功能模块:POI检索,公交方案检索,驾车路线检索,步行路线检索,行政区边界数据检索,地理编码,反地理编码,公交详情检索,在线建议查询,短串分享(包括POI搜索结果分享、驾车/公交/骑行/步行路线规划分享、反向地理编码结果分享)。

    每个检索功能模块都包括一个主检索对象,一个用于构造检索参数的Option结构体,和一个用于接收检索结果回调的Delegate,所有检索服务都使用异步回调模式。使用检索服务时,需要先初始化主检索对象,然后通过主检索对象以包含检索参数的Option做为参数发起检索,最后实现相应的检索功能模块的Delegate处理返回结果 。

    3.1POI检索

    POI(Point of Interest),中文可以翻译为“兴趣点”。在地理信息系统中,一个POI可以是一栋房子、一个商铺、一个邮筒、一个公交站等。

    百度地图SDK提供三种类型的POI检索:周边检索、区域检索和城市内检索。
    周边检索示例:
    定义一个属性,创建一个检索对象,并且懒加载。

    @property (nonatomic, strong) BMKPoiSearch *searcher;//poi检索对象
    -(BMKPoiSearch *)searcher{
        if (_searcher==nil) {
            _searcher =[[BMKPoiSearch alloc]init];
            _searcher.delegate = self;
        }
        return _searcher;
    }
    
    - (void)nearSearch:(BMKUserLocation *)userLocation{
        BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
        
        // 分页显示默认第一页
        option.pageIndex = 0;
        option.pageCapacity = 10;
        
        // 搜索的位置
        //手动指定位置
        //option.location = CLLocationCoordinate2D{23.35, 113.404};
       
        //自动获取当前定位位置
        CLLocation *location = userLocation.location;
        option.location =location.coordinate;
        option.keyword = @"酒店";
        
        // 设置显示区域
        BMKCoordinateSpan span = BMKCoordinateSpanMake(0.03, 0.03);
        BMKCoordinateRegion region = BMKCoordinateRegionMake(option.location, span);
        [_mapView setRegion:region animated:YES];
        
        // 开始搜索
        BOOL flag = [self.searcher poiSearchNearBy:option];
        
        if(flag)
        {
            NSLog(@"周边检索发送成功");
        }
        else
        {
            NSLog(@"周边检索发送失败");
        }   
    }
    
    

    //实现PoiSearchDeleage处理回调结果

     - (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error { 
    if (error == BMK_SEARCH_NO_ERROR) { 
    //在此处理正常结果  }
     else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
     //当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表 
     // result.cityList;  
    NSLog(@"起始点有歧义"); 
    } else {
     NSLog(@"抱歉,未找到结果"); 
    } 
    } 
    
    //不使用时将delegate设置为 nil 
    -(void)viewWillDisappear:(BOOL)animated {
     _searcher.delegate = nil; 
    }
    

    相关文章

      网友评论

          本文标题:iOS项目百度地图的使用2016.6

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