iOS 百度地图

集成百度地图

  • 1.下载框架
  • 2.导入框架到项目
  • 3.问题解决

问题1: Undefined symbols for architecture x86_64: "OBJC_CLASS$_CLLocation", referenced from:
导致原因:没有导入第三方框架要依赖的系统框架
解决方案:比这百度地图开发文档,导入依赖的系统框架

问题2:Undefined symbols for architecture x86_64:"std::terminate()", referenced from:
导致原因:编译器不支持c++环境
解决方案:1. 随意修改一个.m的文件为.mm文件(原因说明:Xcode编译器在编译过程中,会根据当前编译文件的后缀名,自动设定编译环境)
            .c  C代码
            .cpp C++代码
            .m  C代码 + OC代码
            .mm C代码 + OC代码 + C++代码
        2. Project -> Build Settings -> Compile Sources As设置为"Objective-C++"
```
  • 4.补充

    • 1>百度地图在2.5以前是不支持64位手机(如果以后碰到第三方库不支持64位而报错,可作如下修改 :将Architectures修改位:$(ARCHS_STANDARD_32_BIT); 注意:最好直接不用,因为不支持64位,不能上架!)
    • 2> key必须注册成为百度开发者后自己创建,而且必须保证,创建key时填写的bundleID 和项目的 bundleID 一致! 一致! 一致!
  • 5.碰到没法判断的BUG,查看百度地图开发注意事项!!

百度地图的使用

1、封装工具类

  • .h文件
#import 
#import "Singleton.h"
#import //引入所有的头文件

typedef void(^ResultBlock)(NSArray *poiInfos, NSString *error);

@interface JPBaiduTool : NSObject
single_interface(JPBaiduTool)

/**
 *  自定义方法,根据中心还有关键字进行周边检索
 *
 *  @param center      中心
 *  @param key         关键字
 *  @param resultBlock 回调代码块
 */
- (void)poiSearchWithCenter:(CLLocationCoordinate2D)center andKeyWord:(NSString *)key resultBlock:(ResultBlock)resultBlock;

/**
 *  根据传入的中心还有标题和子标题, 添加大头针数据模型到地图
 *
 *  @param center   中心
 *  @param title    标题
 *  @param subTitle 子标题
 *  @param mapView  地图
 */
- (void)addAnnotationWithCenter:(CLLocationCoordinate2D)center title:(NSString *)title subTitle:(NSString *)subTitle toMapView:(BMKMapView *)mapView;

@end
  • 2..m文件
#import "JPBaiduTool.h"

@interface JPBaiduTool()
/** poi检索对象 */
@property (nonatomic, strong) BMKPoiSearch *search;

/** 回调代码块 */
@property (nonatomic, copy) ResultBlock block;

@end

@implementation JPBaiduTool
single_implementation(JPBaiduTool)
#pragma mark -懒加载
-(BMKPoiSearch *)search
{
    if (!_search) {
        _search = [[BMKPoiSearch alloc] init];
        _search.delegate = self;
    }
    return _search;
}

- (void)poiSearchWithCenter:(CLLocationCoordinate2D)center andKeyWord:(NSString *)key resultBlock:(ResultBlock)resultBlock
{
    // 记录block
    self.block = resultBlock;
    
    //发起检索
    BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
    // 第几页
    option.pageIndex = 0;
    // 每一页数量
    option.pageCapacity = 10;
    option.location = center;
    // 搜索关键字
    option.keyword = @"小吃";
    BOOL flag = [self.search poiSearchNearBy:option];
    if(flag)
    {
        NSLog(@"周边检索发送成功");
    }
    else
    {
        NSLog(@"周边检索发送失败");
    }
}

- (void)addAnnotationWithCenter:(CLLocationCoordinate2D)center title:(NSString *)title subTitle:(NSString *)subTitle toMapView:(BMKMapView *)mapView
{
    // 添加大头针
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    annotation.coordinate = center;
    annotation.title = title;
    annotation.subtitle = subTitle;
    [mapView addAnnotation:annotation];
}


#pragma mark -BMKPoiSearchDelegate

//实现PoiSearchDeleage处理回调结果
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        NSLog(@"获取到数据");
        
        // 获取兴趣点列表
        NSArray *pois = poiResultList.poiInfoList;
        self.block(pois, nil);
  
        
 
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
        //当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表
        // result.cityList;
        NSLog(@"起始点有歧义");
        self.block(nil, @"起始点有歧义");
    } else {
         self.block(nil, [NSString stringWithFormat:@"抱歉,未找到结果--%zd", error]);
    }
}
@end

三、使用

#import "ViewController.h"
#import "JPBaiduTool.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet BMKMapView *mapView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置地图代理
    self.mapView.delegate = self;
}

#pragma mark - BMKMapViewDelegate
/**
 *  当长按百度地图时调用
 *
 *  @param mapView    地图
 *  @param coordinate 坐标
 */
-(void)mapview:(BMKMapView *)mapView onLongClick:(CLLocationCoordinate2D)coordinate
{
    [[JPBaiduTool sharedJPBaiduTool] poiSearchWithCenter:coordinate andKeyWord:@"小吃" resultBlock:^(NSArray *poiInfos, NSString *error) {
        // 添加大头针
        
        [poiInfos enumerateObjectsUsingBlock:^(BMKPoiInfo * _Nonnull poiInfo, NSUInteger idx, BOOL * _Nonnull stop) {
            [[JPBaiduTool sharedJPBaiduTool] addAnnotationWithCenter:poiInfo.pt title:poiInfo.name subTitle:poiInfo.address toMapView:mapView];
        }];
  
    }];
  
    // 控制地图显示区域
    BMKCoordinateSpan span = BMKCoordinateSpanMake(0.01, 0.1);
    BMKCoordinateRegion region = BMKCoordinateRegionMake(coordinate, span);
    [mapView setRegion:region animated:YES];
}
@end

你可能感兴趣的:(iOS 百度地图)