上一篇文章中通過一系列操作,已經成功設置了工程。那麼接下來就是使用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中。