ios百度地圖定位實現

附上百度地圖基礎地圖實現:http://blog.csdn.net/fantasy_jun/article/details/53979887

一、定位顯示類型

4.1起,新增一種定位方法BMKUserTrackingModeHeading,普通定位+定位羅盤模式。
目前爲止,BMKMapView的定位模式(userTrackingMode)有4種分別是:
1) BMKUserTrackingModeNone :普通定位模式,顯示我的位置,我的位置圖標和地圖都不會旋轉
2) BMKUserTrackingModeFollow : 定位跟隨模式,我的位置始終在地圖中心,我的位置圖標會旋轉,地圖不會旋轉
3) BMKUserTrackingModeFollowWithHeading : 定位羅盤模式,我的位置始終在地圖中心,我的位置圖標和地圖都會跟着旋轉

4) BMKUserTrackingModeHeading:普通定位+定位羅盤模式,顯示我的位置,我的位置始終在地圖中心,我的位置圖標會旋轉,地圖不會旋轉。即在普通定位模式的基礎上顯示方向。


二、單獨獲取位置信息(不顯示在地圖上)

定位功能可以和地圖功能分離使用,單獨的定位功能使用方式如下:

1.選用#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的頭文件

2.添加代理<BMKLocationServiceDelegate>

3.代碼部分

-(void)viewDidLoad
{ 
//初始化BMKLocationService 
_locService = [[BMKLocationService alloc]init]; 
_locService.delegate = self; 
//啓動LocationService 
[_locService startUserLocationService]; 
} 
//實現相關delegate 處理位置信息更新 
//處理方向變更信息 
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation 
{ 
//NSLog(@"heading is %@",userLocation.heading); 
} 
//處理位置座標更新 
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation 
{ 
//NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude); 
}


三、展示定位信息(顯示在地圖上)

1.選用頭文件

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相關所有的頭文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地圖功能所有的頭文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的頭文件
#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的單個頭文件

2.添加代理

<BMKMapViewDelegate,BMKLocationServiceDelegate>


3.代碼部分

.h文件
@interface FJMapVC : UIViewController<BMKMapViewDelegate,BMKLocationServiceDelegate>
{
    BMKMapView* mapView;//地圖
    BMKLocationService *_locService;//定位
}
@end
.m文件
@implementation FJMapVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
    
    [self setupLocation];
}

-(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;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setupView
{
    [self.view setBackgroundColor:[UIColor blueColor]];
    
    mapView = [[BMKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds]; 
    self.view = mapView;

}

//初始化定位
-(void)setupLocation
{
    //自定義精度圈
    BMKLocationViewDisplayParam *param = [[BMKLocationViewDisplayParam alloc] init];
    //線
    param.accuracyCircleStrokeColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.5];
    //圈
//    param.accuracyCircleFillColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.2];
    [mapView updateLocationViewWithParam:param];
    [mapView setZoomLevel:13];
    
    //初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    //啓動LocationService
    [_locService startUserLocationService];
    mapView.showsUserLocation = NO;//先關閉顯示的定位圖層
    mapView.userTrackingMode = BMKUserTrackingModeFollow;//設置定位的狀態
    mapView.showsUserLocation = YES;//顯示定位圖層
    
}

#pragma mark - 定位代理
/**
 *在地圖View將要啓動定位時,會調用此函數
 *地圖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];
}

/**
 *在地圖View停止定位後,會調用此函數
 * 地圖View
 */
- (void)didStopLocatingUser
{
//    NSLog(@"stop locate");
}

/**
 *定位失敗後,會調用此函數
 *地圖View
 *@param error 錯誤號,參考CLError.h中定義的錯誤號
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"location error");
}
- (void)dealloc {
    if (mapView) {
        mapView = nil;
    }
}


@end



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章