百度SDK下载地址:
*http://lbsyun.baidu.com/apiconsole/key
使用步骤: (注意: 是在纯代码的基础上进行的, 如果使用StoryBoard和Xib另当别论)
-
创建应用:需要注意两点(指定项目名 + 找到项目中general--> Bundle Identifier--> 复制到安全码处)
-
静态库中采用ObjectC++实现, 需要工程中有一个文件的后缀为.mm的源文件(任意一个.m文件改为.mm 即可)
-
配置info.plist文件下面两选项
咣咣 !配置完成, 下面实现代码部分
AppDelegate.m文件
#import "AppDelegate.h"
#第一步: 导入头文件
#import <BaiduMapAPI_Base/BMKMapManager.h>
@interface AppDelegate ()
#第二步: 创建管理者
@property (strong, nonatomic) BMKMapManager * mapManager;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#pragma mark-- 百度地图
// 要使用百度地图,请先启动BaiduMapManager
_mapManager = [[BMKMapManager alloc]init];
// 如果要关注网络及授权验证事件,请设定 generalDelegate参数
BOOL ret = [_mapManager start:@"yFiVBGc1DIznyAxKeUIXWCwrdDcT7YXl" generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
}
return YES;
}
#import "MapViewController.h"
@interface MapViewController ()<BMKMapViewDelegate,BMKGeoCodeSearchDelegate>
@property (nonatomic,strong)BMKGeoCodeSearch * geoCodeSearch;//编码搜索对象
@end
@implementation MapViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
self.mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
self.mapView.zoomLevel = 17;//比例
[self.view addSubview: self.mapView];
//检索工具
self.geoCodeSearch = [[BMKGeoCodeSearch alloc] init];
_geoCodeSearch.delegate = self;
//(检索信息)类对象
BMKGeoCodeSearchOption * option = [[BMKGeoCodeSearchOption alloc] init];
option.city = @"北京市";
option.address = @"朝阳区亮马家园35号楼";
BOOL flag = [_geoCodeSearch geoCode:option];
if (flag) {
NSLog(@"检索成功!");
}else
{
NSLog(@"检索失败!");
}
}
#pragma mark---绘制大头针
//根据annotation绘制大头针
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
NSString * annotationViewID = @"annotationID";
//根据指定标识查找一个可被复用的annotation
BMKAnnotationView * annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewID];
if (!annotationView) {
annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewID];
((BMKPinAnnotationView *)annotationView).pinColor = BMKPinAnnotationColorPurple;
((BMKPinAnnotationView *)annotationView).animatesDrop = YES;
}
//
// annotationView.centerOffset = CGPointMake(0, 10);
annotationView.annotation = annotation;
annotationView.canShowCallout = YES;
return annotationView;
}
#pragma mark -- 协议方法
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
NSLog(@"%@",result);
//将上一次大头针数据清空
NSArray * array = [NSArray arrayWithArray:_mapView.annotations];
[_mapView removeAnnotations:array];
array = [NSArray arrayWithArray:_mapView.overlays];
[_mapView removeOverlays:array];
BMKPointAnnotation * pointAnnotation = [[BMKPointAnnotation alloc] init];
//Annotaion在MVC中相当于M,存储了大头针的相关信息
//设置经纬度
pointAnnotation.coordinate = result.location;
pointAnnotation.title = result.address;
[_mapView addAnnotation:pointAnnotation];
//设置当前地图的中心点
_mapView.centerCoordinate = result.location;
}
- (void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
}
-(void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用时,置nil
}
@end