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

百度地图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");
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值