iOS 百度地图定位并标注当前位置

第一步:获取安全码(获取方法这里不做介绍)

 

第二步:使用CocoaPods配置

pod 'BaiduMapKit' #百度地图SDK

 

第三步:需要引入的头文件

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件

#import <BaiduMapApi_Map/BMKMapView.h>
#import <BaiduMapApi_Map/BMKPointAnnotation.h>

 

第四步:在info.plist文件中加入

<key>NSLocationWhenInUseUsageDescription</key>
    <string>是否开启定位查看当前所在位置</string>

再设置好项目的 Bundle display name

 

第五步:在AppDelegate.m里

#import "AppDelegate.h"

@interface AppDelegate ()

@property (nonatomic, strong) BMKMapManager *mapManager;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.mapManager = [BMKMapManager new];
    BOOL ret = [self.mapManager start:BMK_KEY generalDelegate:nil];
    
    if (!ret){
        NSLog(@"百度地图启动失败");
        
    }else{
        NSLog(@"百度地图启动成功");
    }
    
    return YES;
}

 

最后,在需要定位的控制器里

#import "ViewController.h"

@interface ViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate>
{
    BMKMapView *mapView;
}

///定位服务对象
@property (nonatomic, strong) BMKLocationService *locService;

@end

@implementation ViewController

- (BMKLocationService *)locService{
    if (!_locService)
    {
        _locService = [[BMKLocationService alloc] init];
        _locService.delegate = self;
    }
    return _locService;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [mapView setMapType:BMKMapTypeStandard];
    mapView.zoomLevel = 18;//地图等级,数字越大越清晰
    mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态为普通定位模式
    //设置mapView的代理
    mapView.delegate = self;
    //将mapView添加到当前视图中
    [self.view addSubview:mapView];
    
    //启动百度地图定位服务
    [self.locService startUserLocationService];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    //当mapView即将被显示的时候调用,恢复之前存储的mapView状态
    [mapView viewWillAppear];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [mapView viewWillDisappear];
    //释放内存
    mapView.delegate = nil;
}

#pragma mark - BMKLocationServiceDelegate
/**
 定位失败后,会调用此函数

 @param error 错误
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"百度地图定位失败%@",error);
}

/**
 处理位置坐标更新
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
        NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,
              userLocation.location.coordinate.longitude);
    
    //关闭坐标更新
    [self.locService stopUserLocationService];
    
    //将坐标定为地图中点
    CLLocationCoordinate2D  coordinate = CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
    mapView.centerCoordinate = coordinate;
    
    //创建标注
    [self createAnnotationWithLatitude:userLocation.location.coordinate.latitude longitude:userLocation.location.coordinate.longitude];
}

#pragma mark - 创建标注
-(void)createAnnotationWithLatitude:(double)latitude longitude:(double)longitude{
    
    BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc]init];
    annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
    //执行 addAnnotation:方法后才会走<BMKMapViewDelegate>代理方法
    [mapView addAnnotation:annotation];
}

#pragma mark - <BMKMapViewDelegate>
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{
    
    BMKAnnotationView *annotationView = [[BMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"];
    annotationView.image = [UIImage imageNamed:@"hd_location"];
    return annotationView;
}


@end

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值