上一篇文章中通过一系列操作,已经成功设置了工程。那么接下来就是使用framework了。
AppDelegate
按着文档设置的,直接上代码
//AppDelegate.h
#import <UIKit/UIKit.h>
#import <BaiduMapAPI/BMKMapView.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>{
UINavigationController *navigationController;
BMKMapManager* _mapManager;
}
@property (strong, nonatomic) UIWindow *window;
@end
注意 AppDelegate.m 里需要输入自己申请的key
//AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_mapManager = [[BMKMapManager alloc]init];
// 如果要关注网络及授权验证事件,请设定 generalDelegate 参数
BOOL ret = [_mapManager start:@"在这里输入key" generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
}
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
[BMKMapView willBackGround];//当应用即将后台时调用,停止一切调用opengl相关的操作
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[BMKMapView didForeGround];//当应用恢复前台状态时调用,回复地图的渲染和opengl相关的操作
}
MapViewController
一样直接上代码,
//MapViewController.h
#import <UIKit/UIKit.h>
#import <BaiduMapAPI/BMKMapView.h>
@interface MapViewController : UIViewController<BMKMapViewDelegate>
@end
//MapViewController.m
#import "MapViewController.h"
@interface MapViewController (){
BMKMapView* _mapView;
}
@end
@implementation MapViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view = _mapView;
}
-(void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
}
-(void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用时,置nil
}
在storyboard里把进入的ViewController改为MapViewController,运行几次,地图也只显示格子的底色。
再仔细看了文档,发现项目的bundle id跟我当时申请key时用的不一样,恍然大悟,到官网查看key,然后点设置——看见安全码,将工程里的bundle id改过来,就可以了。
这时候运行出来会显示北京的地图,但是我人在成都,就需要定位功能来实现运行后直接显示成都地图。
定位
到MapViewController.h中添加
#import <BaiduMapAPI/BMKLocationService.h>
再添加BMKLocationServiceDelegate。
到MapViewController.m中
添加公用的
BMKLocationService *_locService;
然后在 viewDidLoad 方法里添加
//设置定位精确度,默认:kCLLocationAccuracyBest
[BMKLocationService setLocationDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
//指定最小距离更新(米),默认:kCLDistanceFilterNone
[BMKLocationService setLocationDistanceFilter:100.f];
//初始化BMKLocationService
_locService = [[BMKLocationService alloc]init];
_locService.delegate = self;
//启动LocationService
[_locService startUserLocationService];
//罗盘态
_mapView.showsUserLocation = NO;
_mapView.userTrackingMode = BMKUserTrackingModeFollow;
_mapView.showsUserLocation = YES;
再实现两个委托方法
//实现相关delegate 处理位置信息更新
//处理方向变更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
[_mapView updateLocationData:userLocation];
//NSLog(@"heading is %@",userLocation.heading);
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
[_mapView updateLocationData:userLocation];
//NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
}
运行后会有系统提示,是否使用当前位置,选择“好”。
运行,定位成功~
上面的工程可以在这里下载试用。
试用时注意:
1.bundle id的填写要与申请key时填写的安全码一致。
2.要把自己的key填进AppDelegate.m中。