开发环境: Xcode6.3.2GM 百度地图iOS SDK: v2.7.0
目录
一. 前期准备
- 注册成为开发者, 申请Key
- 查看官网的注意事项, 执行必要项
- 配置开发环境(配置.framework形式开发包)
二. Hello BaiduMap
- 显示出mapView
- 一些简单的功能按钮
- 添加Annotation
- 搜索地点
- 公交路线
- 解析地址
- 反向地理编码结果
一. 前期准备
1. 注册成为开发者, 申请Key
2. 查看官网的注意事项, 执行必要项
1.保证工程中至少有一个.mm后缀的源文件
4.管理地图的生命周期(不确定是否需要执行该条)
5.NSLocationWhenInUseUsageDescription
6.因为我们使用的是Xcode6版本, 在info.plist中添加:Bundle display name ,且其值不能为空
3. 配置开发环境(配置.framework形式开发包)
1.导入BaiduMapAPI.framework(注意模拟器和真机的区别)
2.引入所需的系统库CoreLocation.framework
、QuartzCore.framework
、OpenGLES.framework
、SystemConfiguration.framework
、CoreGraphics.framework
、Security.framework
, 添加方式:在Xcode的Project -> Target ->Build Phases ->Link Binary With Libraries
- 环境配置: 在TARGETS->Build Settings->Other Linker Flags 中添加-ObjC(注意大小写)
- 导入mapapi.bundle资源文件
- 在需要的文件中引入头文件
#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];
}
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);
}
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(@"抱歉,未找到结果");
}
}
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(@"抱歉,未找到结果");
}
}
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(@"抱歉,未找到结果");
}
}