您的位置:首页 > 移动开发 > IOS开发

iOS开发 -- 百度地图api的使用

2015-10-23 19:01 399 查看
百度地图api

http://developer.baidu.com/map/index.php?title=iossdk/guide/key

下载百度iOS地图SDK

下载framework形式静态库



申请密钥 申请步骤上面链接有详细解释



获取密钥



然后配置开发环境

注意:一般配置的framework所支持的架构同时支持真机和模拟器使用,

然后把xcode里的文件一个.m文件改为.mm文件 以AppDelegate.m为例

然后在xcode工程里面导入 mapapi.bundle文件以及系统.framework文件



然后在AppDelegate.m文件里 写如下代码

#import "AppDelegate.h"

#import <BaiduMapAPI/BMapKit.h>  //引入所有的头文件
//#import <BaiduMapAPI/BMKMapView.h>  //只引入所需的单个头文件

@interface AppDelegate ()<BMKGeneralDelegate>

@end

BMKMapManager* _mapManager;

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// 要使用百度地图,请先启动BaiduMapManager
_mapManager = [[BMKMapManager alloc]init];

BOOL ret = [_mapManager start:@"wLWgKV8elQKI0OtHRGFgf2so" generalDelegate:self];

if (!ret) {
NSLog(@"manager start failed!");
}

return YES;
}


然后在控制器里.h文件写

注意:在使用SDK的类引入头文件:

#import <BaiduMapAPI/BMapKit.h>//引入所有的头文件

#import <BaiduMapAPI/BMKMapView.h>//只引入所需的单个头文件

#import <UIKit/UIKit.h>
#import <BaiduMapAPI/BMapKit.h>

@interface ViewController : UIViewController<BMKMapViewDelegate,BMKLocationServiceDelegate>
{
//_mapView是自定义的一个UIView控件
__weak IBOutlet BMKMapView *_mapView;

BMKLocationService* _locService;

}
@end


在控制器里.m文件写

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

//纬度40.030401  经度116.343569
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

_locService = [[BMKLocationService alloc]init];

[_locService startUserLocationService];
_mapView.showsUserLocation = NO;//先关闭显示的定位图层
_mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态
_mapView.showsUserLocation = YES;//显示定位图层

}

-(void)viewWillAppear:(BOOL)animated {
[_mapView viewWillAppear];
_mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
_locService.delegate = self;
}

-(void)viewWillDisappear:(BOOL)animated {
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用时,置nil
_locService.delegate = nil;
}

/**
*在地图View将要启动定位时,会调用此函数
*@param mapView 地图View
*/
- (void)willStartLocatingUser
{
NSLog(@"start locate");
}

/**
*用户方向更新后,会调用此函数
*@param userLocation 新的用户位置
*/
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
[_mapView updateLocationData:userLocation];
NSLog(@"heading is %@",userLocation.heading);
}

/**
*用户位置更新后,会调用此函数
*@param userLocation 新的用户位置
*/
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
//    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
[_mapView updateLocationData:userLocation];
}

/**
*定位失败后,会调用此函数
*@param mapView 地图View
*@param error 错误号,参考CLError.h中定义的错误号
*/
- (void)didFailToLocateUserWithError:(NSError *)error
{
NSLog(@"location error");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: