(八十四)集成百度地图和基本使用
配置的具体内容能够查看百度地图API文档来获取。
①首先下载百度iOS SDK的完整包,将当中Lib的Release-iphonesimulator下的framework文件导入到project,然后双击打开framework文件。再打开Resources,将当中的mapapi.bundle导入到project。
②从系统设置中导入下列框架:
③在Build Settings里搜索other linker Flags,加入-ObjC标记。
④由于静态库用到了C++。因此project中必须有mm文件,最简单的方法是把当中一个m文件改成mm。
⑤对于Xcode6和以后的版本号,还须要在info.plist中增加Bundle display name,值随便写。一般写Bundle identifier的值。
⑥在AppDelegate中创建管理者。然后启动,须要传入key。
#import "AppDelegate.h"
@interface AppDelegate ()
@property (nonatomic, strong) BMKMapManager *mapManager;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_mapManager = [[BMKMapManager alloc] init];
BOOL ret = [_mapManager start:@"这里填写key" generalDelegate:self];
if (!ret) {
NSLog(@"manager start failed");
}
return YES;
}
@endTip:key和bundle identify的值应该和申请开发人员时创建的应用内的一致。否则无法正常的授权。
⑦显示地图和基本使用
以下的代码创建了MapView和Poisearch。实现了地图的显示和POI检索。
#import "ViewController.h"
#import <BaiduMapAPI/BMapKit.h>
@interface ViewController () <BMKMapViewDelegate,BMKPoiSearchDelegate>
@property (nonatomic, weak) BMKMapView *mapView;
@property (nonatomic, strong) BMKPoiSearch *searcher;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 20, 320, 420)];
[self.view addSubview:mapView];
_mapView = mapView;
_searcher = [[BMKPoiSearch alloc] init];
_searcher.delegate = self;
}
- (void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此处记得不用的时候须要置nil。否则影响内存的释放
}
- (void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用时,置nil
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"in");
// 检索周边信息
BMKCitySearchOption *option = [[BMKCitySearchOption alloc] init];
option.pageIndex = 0;
option.pageCapacity = 10;
option.city = @"北京";
option.keyword = @"小吃";
BOOL flag = [_searcher poiSearchInCity:option];
if(flag)
{
NSLog(@"周边检索发送成功");
}
else
{
NSLog(@"周边检索发送失败");
}
}
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult*)result errorCode:(BMKSearchErrorCode)error
{
// 清楚屏幕中全部的annotation
NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
[_mapView removeAnnotations:array];
if (error == BMK_SEARCH_NO_ERROR) {
NSMutableArray *annotations = [NSMutableArray array];
for (int i = 0; i < result.poiInfoList.count; i++) {
BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];
BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
item.coordinate = poi.pt;
item.title = poi.name;
[annotations addObject:item];
}
[_mapView addAnnotations:annotations];
[_mapView showAnnotations:annotations animated:YES];
} else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){
NSLog(@"起始点有歧义");
} else {
// 各种情况的推断。。。
}
}
@end
浙公网安备 33010602011771号