您的位置:首页 > 其它

(八十四)集成百度地图和基本使用

2015-07-23 21:29 267 查看
配置的详细内容可以查看百度地图API文档来获取。

①首先下载百度iOS SDK的完整包,将其中Lib的Release-iphonesimulator下的framework文件导入到工程,然后双击打开framework文件,再打开Resources,将其中的mapapi.bundle导入到工程。

②从系统设置中导入下列框架:



③在Build Settings里搜索other linker Flags,添加-ObjC标记。

④因为静态库用到了C++,因此工程中必须有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;
}

@end


Tip: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: