D28:百度地图iOS SDK使用

作者: Vinc | 来源:发表于2015-06-11 09:01 被阅读1544次

开发环境: Xcode6.3.2GM 百度地图iOS SDK: v2.7.0

目录

一. 前期准备

  1. 注册成为开发者, 申请Key
  2. 查看官网的注意事项, 执行必要项
  3. 配置开发环境(配置.framework形式开发包)

二. Hello BaiduMap

  1. 显示出mapView
  2. 一些简单的功能按钮
  3. 添加Annotation
  4. 搜索地点
  5. 公交路线
  6. 解析地址
  7. 反向地理编码结果

一. 前期准备

1. 注册成为开发者, 申请Key

2. 查看官网的注意事项, 执行必要项

1.保证工程中至少有一个.mm后缀的源文件
4.管理地图的生命周期(不确定是否需要执行该条)
5.NSLocationWhenInUseUsageDescription
6.因为我们使用的是Xcode6版本, 在info.plist中添加:Bundle display name ,且其值不能为空

3. 配置开发环境(配置.framework形式开发包)

1.导入BaiduMapAPI.framework(注意模拟器和真机的区别)
2.引入所需的系统库CoreLocation.frameworkQuartzCore.frameworkOpenGLES.frameworkSystemConfiguration.frameworkCoreGraphics.frameworkSecurity.framework, 添加方式:在Xcode的Project -> Target ->Build Phases ->Link Binary With Libraries

  1. 环境配置: 在TARGETS->Build Settings->Other Linker Flags 中添加-ObjC(注意大小写)
  2. 导入mapapi.bundle资源文件
  3. 在需要的文件中引入头文件#import <BaiduMapAPI/BMapKit.h>//引入所有的头文件

二. Hello BaiduMap

1. 显示出mapView
#import "ViewController.h"
#import <BaiduMapAPI/BMapKit.h> // 引入所有的头文件

#define kBDMapKey (@"6RFRv4EiqQbn6V0IhDiA7zjc")

@interface ViewController () 
{
    BMKMapManager *_mapManager;
    BMKMapView *_mapView;
}

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _mapManager = [[BMKMapManager alloc]init];
    
    // 如果要关注网络及授权验证事件,请设定 generalDelegateb参数
    BOOL ret = [_mapManager start:kBDMapKey  generalDelegate:nil];
    if (!ret) {
        NSLog(@"manager start failed!");
    } else {
        NSLog(@"manager has started!");
    }
    
    [self createMapView];
}

- (void)createMapView
{
    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 100, 320, 468)];
    [self.view addSubview:_mapView];
}
显示地图.png
2. 一些简单的功能按钮
- (void)viewDidLoad {
    …………………………………………………………………………………………  
    
    [self createBtns];
}

- (void)createBtns
{
    UIButton *addSearchButton = [MyUtility createButtonWithFrame:CGRectMake(10, 30, 60, 20) title:@"搜地址" backgroundImageName:nil target:self action:@selector(searchPalce)];
    UIButton *geocoderButton = [MyUtility createButtonWithFrame:CGRectMake(80, 30, 60, 20) title:@"解析" backgroundImageName:nil target:self action:@selector(geocoderAction)];
    UIButton *reverseGeocoderButton = [MyUtility createButtonWithFrame:CGRectMake(150, 30, 60, 20) title:@"反解析" backgroundImageName:nil target:self action:@selector(reverseGeocoderAction)];
    UIButton *mapTypeButton = [MyUtility createButtonWithFrame:CGRectMake(220, 30, 60, 20) title:@"卫星图" backgroundImageName:nil target:self action:@selector(mapTypeAction)];
    UIButton *trafficButton = [MyUtility createButtonWithFrame:CGRectMake(10, 60, 60, 20) title:@"路况" backgroundImageName:nil target:self action:@selector(trafficAction)];
    UIButton *heatMapButton = [MyUtility createButtonWithFrame:CGRectMake(80, 60, 60, 20) title:@"热力图" backgroundImageName:nil target:self action:@selector(heatMapAction)];
    
    [self.view addSubview:addSearchButton];
    [self.view addSubview:geocoderButton];
    [self.view addSubview:reverseGeocoderButton];
    [self.view addSubview:mapTypeButton];
    [self.view addSubview:trafficButton];
    [self.view addSubview:heatMapButton];
}

// 是否打开热力图
- (void)heatMapAction
{
    //打开百度城市热力图图层(百度自有数据)
    // [_mapView setBaiduHeatMapEnabled:YES];
    (_mapView.baiduHeatMapEnabled == 1)? (_mapView.baiduHeatMapEnabled = 0): (_mapView.baiduHeatMapEnabled = 1);
}

// 是否打开路况
- (void)trafficAction
{
    // [_mapView setTrafficEnabled:YES];
    (_mapView.trafficEnabled == 1)? (_mapView.trafficEnabled = 0): (_mapView.trafficEnabled = 1);
}

// 地图类型
- (void)mapTypeAction
{
    /*
     BMKMapTypeStandard   = 1,               ///< 标准地图
     BMKMapTypeSatellite  = 2
     */
    (_mapView.mapType == 1)? (_mapView.mapType = 2): (_mapView.mapType = 1);
}  
切换卫星图/普通视图.png 路况.png 热力图.png
3. 添加Annotation
@interface ViewController () <BMKMapViewDelegate>  
…………………………………………………………………………………………  

- (void)viewDidLoad {
    …………………………………………………………………………………………  
    
    [self createAnnotations];
}

- (void)createAnnotations
{
    // 添加PointAnnotation
    BMKPointAnnotation *annotationJiaxing = [[BMKPointAnnotation alloc]  init];
    annotationJiaxing.coordinate = CLLocationCoordinate2DMake(30.77, 120.76);
    annotationJiaxing.title = @"嘉兴";
    
    BMKPointAnnotation *annotationShanghai = [[BMKPointAnnotation alloc]  init];
    annotationShanghai.coordinate = CLLocationCoordinate2DMake(31, 121);
    annotationShanghai.title = @"上海";
    
    [_mapView addAnnotation:annotationJiaxing];
    [_mapView addAnnotation:annotationShanghai];
}  

#pragma mark - BMKMapViewDelegate
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
        return newAnnotationView;
    }
    
    return nil;
}
4. 搜索地点
@interface ViewController () <BMKMapViewDelegate, BMKPoiSearchDelegate>
{
    BMKMapManager *_mapManager;
    BMKMapView *_mapView;
    BMKPoiSearch *_searcher;
}

- (void)searchPalce
{
    //初始化检索对象
    _searcher =[[BMKPoiSearch alloc]init];
    _searcher.delegate = self;
    //发起检索
    BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
    option.pageCapacity = 10;
    option.location = CLLocationCoordinate2DMake(39.915, 116.404);
    option.keyword = @"江南";
    BOOL flag = [_searcher poiSearchNearBy:option];
    if (flag)
    {
        NSLog(@"周边检索发送成功");
    }
    else
    {
        NSLog(@"周边检索发送失败");
    }
}  
  
#pragma mark - BMKPoiSearchDelegate
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        
        NSMutableArray *array = [NSMutableArray array];
        for (BMKPoiInfo *poi in poiResultList.poiInfoList) {
            // 创建一个大头针对象
            BMKPointAnnotation *anno = [[BMKPointAnnotation alloc] init];
            anno.coordinate = CLLocationCoordinate2DMake(poi.pt.latitude, poi.pt.longitude);
            anno.title = poi.name;
            anno.subtitle = poi.city;
            
            [array addObject:anno];
        }
        
        // 移除之前的大头针
        [_mapView removeAnnotations:_mapView.annotations];
        // 添加到地图上
        [_mapView addAnnotations:array];
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
        //当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表
        // result.cityList;
        NSLog(@"起始点有歧义");
    } else {
        NSLog(@"抱歉,未找到结果");
    }
}
搜地址.png
5. 公交路线
@interface ViewController <……, BMKRouteSearchDelegate>  
- (void)createBtns
{
    …………………………………………………………………………………………
    UIButton *metroButton = [MyUtility createButtonWithFrame:CGRectMake(150, 60, 60, 20) title:@"公交" backgroundImageName:nil target:self action:@selector(metroAction)];
    [self.view addSubview:metroButton];
}  
  
- (void)metroAction
{
    //初始化检索对象
    BMKRouteSearch *routeSearch = [[BMKRouteSearch alloc]init];
    routeSearch.delegate = self;

    //发起检索
    BMKPlanNode* start = [[BMKPlanNode alloc] init] ;
    start.name = @"水产路";
    BMKPlanNode* end = [[BMKPlanNode alloc] init];
    end.name = @"江浦路";
    BMKTransitRoutePlanOption *transitRouteSearchOption = [[BMKTransitRoutePlanOption alloc]init];
    transitRouteSearchOption.city= @"上海市";
    transitRouteSearchOption.from = start;
    transitRouteSearchOption.to = end;
    BOOL flag = [routeSearch transitSearch:transitRouteSearchOption];
    
    if(flag)
    {
        NSLog(@"bus检索发送成功");
    }
    else
    {
        NSLog(@"bus检索发送失败");
    }
}
  
#pragma mark - BMKRouteSearchDelegate
-(void)onGetTransitRouteResult:(BMKRouteSearch*)searcher result:(BMKTransitRouteResult*)result
                     errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        
        for (BMKTransitRouteLine *line in result.routes) {
//            ///路线长度 单位: 米
//            @property (nonatomic) int distance;
//            ///路线耗时 单位: 秒
//            @property (nonatomic, strong) BMKTime* duration;
//            ///路线起点信息
//            @property (nonatomic, strong) BMKRouteNode* starting;
//            ///路线终点信息
//            @property (nonatomic, strong) BMKRouteNode* terminal;
            NSString *routeDescription = [NSString stringWithFormat:@"路程长度: %d\n路线耗时: %d小时%d分%d秒", line.distance, line.duration.hours, line.duration.minutes, line.duration.seconds];
            NSLog(@"%@", routeDescription);
        }
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){
        //当路线起终点有歧义时通,获取建议检索起终点
        //result.routeAddrResult
    }
    else {
        NSLog(@"抱歉,未找到结果");
    }
}
6. 解析地址
@interface ViewController () <……, BMKGeoCodeSearchDelegate>  
- (void)geocoderAction
{
    BMKGeoCodeSearch *geocoderSearcher =[[BMKGeoCodeSearch alloc]init];
    geocoderSearcher.delegate = self;
    BMKGeoCodeSearchOption *geoCodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];
    geoCodeSearchOption.city= @"上海市";
    geoCodeSearchOption.address = @"田子坊";
    BOOL flag = [geocoderSearcher geoCode:geoCodeSearchOption];
    if (flag)
    {
        NSLog(@"geo检索发送成功");
    }
    else
    {
        NSLog(@"geo检索发送失败");
    }

}
  
#pragma mark - BMKGeoCodeSearchDelegate
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        
        // 创建大头针
        BMKPointAnnotation *anno = [[BMKPointAnnotation alloc] init];
        anno.coordinate = CLLocationCoordinate2DMake(result.location.latitude, result.location.longitude);
        anno.title = result.address;
            
        // 移除之前的大头针
        [_mapView removeAnnotations:_mapView.annotations];
        // 添加到地图上
        [_mapView addAnnotation:anno];

    }
    else {
        NSLog(@"抱歉,未找到结果");
    }
}
解析地址.png
7. 反向地理编码结果
- (void)reverseGeocoderAction
{
//    (latitude = 31.214260914625513, longitude = 121.47498064783355)
//    乌镇: 东经120°54′,北纬30°64′
    
    BMKGeoCodeSearch *geocoderSearcher =[[BMKGeoCodeSearch alloc]init];
    geocoderSearcher.delegate = self;

    //发起反向地理编码检索
    CLLocationCoordinate2D pt = (CLLocationCoordinate2D){30.5, 120.5};
    BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeOption alloc] init];
    reverseGeoCodeSearchOption.reverseGeoPoint = pt;
    BOOL flag = [geocoderSearcher reverseGeoCode:reverseGeoCodeSearchOption];
    if(flag)
    {
      NSLog(@"反geo检索发送成功");
    }
    else
    {
      NSLog(@"反geo检索发送失败");
    }
}
  
// 接收反向地理编码结果
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result
errorCode:(BMKSearchErrorCode)error {
  if (error == BMK_SEARCH_NO_ERROR) {
      
      // 在此处理正常结果
      NSLog(@"%@", result.address);
      
      //发起检索
      BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
      option.pageCapacity = 10;
      option.location = result.location;
      option.keyword = @"乌镇";
      BOOL flag = [_searcher poiSearchNearBy:option];
      if (flag)
      {
          NSLog(@"周边检索发送成功");
      }
      
  }
  else {
      NSLog(@"抱歉,未找到结果");
  }
}
反解析地址.png

相关文章

  • D28:百度地图iOS SDK使用

    开发环境: Xcode6.3.2GM 百度地图iOS SDK: v2.7.0目录 一. 前期准备 注册成为开发...

  • 百度地图之导航bug

    参考:百度地图SDK和百度导航SDK合并冲突问题 百度地图sdk和导航sdk共同使用 百度导航SDK是用于导航的,...

  • 百度地图Flutter SDK踩坑记

    引言: 使用百度地图Flutter SDK,报错: 详细: 百度地图刚发布了Flutter版的SDK,正巧赶上之前...

  • 解决百度地图找不到.a文件的错误

    iOS百度地图SDK的使用,BMapKit报错问题 在你的项目中如果报的错误是:library not found...

  • 关于新版本百度地图SDK的一些坑

    因工程需要,在公司新项目需要使用到新版本的百度地图SDK,新版本的百度地图SDK和之前老版本的百度地图SDK存在一...

  • 百度地图之官方demo的坑

    参考 iOS百度地图 Demo IOS SDK百度地图不能正常显示,只显示网格 刚下下来的是报错的,要执行以下几步...

  • iOS 百度地图v2.9.1 API 的详细使用(二)

    基础地图 开发者可利用SDK提供的接口,使用百度提供的基础地图数据。目前百度地图SDK所提供的地图等级为19级,所...

  • iOS 集成高德地图

    参考文档:iOS 跳转方式实现地图导航功能 应用内导航 是指使用地图服务提供的SDK(比如高德,百度等等),直接将...

  • [iOS]百度地图整理

    开发者可在百度地图iOS SDK的下载页面下载到最新版的地图SDK,下载地址为:http://developer....

  • iOS集成百度地图之整理

    开发者可在百度地图iOS SDK的下载页面下载到最新版的地图SDK,下载地址为:http://developer....

网友评论

  • 薛定谔的黑猫警长:点击公交为什么一直显示检索失败
    Vinc:@薛定谔的黑猫警长 这篇文章太老了,已经2年多没碰百度地图了,建议找找别的资料,或者自己再多Debug一下
  • 863c73f31933:大神,我这边在iOS7.0.4上一运行就闪退
    Vinc:当不起大神的称呼,这个教程快2年了挺老了,你对着新版SDK的教程好好检查一下看看
  • 春田花花幼儿园:总结的很好啊,现在9.3和百度升级之后,还需要导入另外三个frmework.大家注意下
    言溪Lee:@春田花花幼儿园_沐然 请问你说的另外三个是指什么?
  • PPAbner:大神能不能发下你的demo看看,现在正在学习!!!chinesemanbobo@163.com

本文标题:D28:百度地图iOS SDK使用

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