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

iOS百度地图定位功能iOS8+

2015-10-18 19:12 483 查看
前提:在info.plist中加入:

NSLocationAlwaysUsageDescription=YES

NSLocationWhenInUseUsageDescription=YES

#import <UIKit/UIKit.h>

#import <BaiduMapAPI/BMapKit.h>

@interface BaiduMainViewController :UIViewController<BMKMapViewDelegate,BMKLocationServiceDelegate>

@property(strong,nonatomic)BMKLocationService *locService;

@end

BMKMapView* mapView;

- (void)viewDidLoad {

[superviewDidLoad];

//适配ios7

if( ([[[UIDevicecurrentDevice]
systemVersion]doubleValue]>=7.0))

{

self.navigationController.navigationBar.translucent
= NO;

}

if ([[UIDevicecurrentDevice].systemVersionfloatValue]
>= 8) {

//由于IOS8中定位的授权机制改变需要进行手动授权

CLLocationManager *locationManager = [[CLLocationManageralloc]
init];

//获取授权认证

[locationManager requestAlwaysAuthorization];

[locationManager requestWhenInUseAuthorization];

}

mapView = [[BMKMapViewalloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

self.view =mapView;

[self startLocation];

}

-(void)startLocation{

if(_locService==nil){

_locService=[[BMKLocationServicealloc]init];

}

_locService.delegate=self;

[_locService
startUserLocationService];

mapView.userTrackingMode=BMKUserTrackingModeFollow;

}

#pragma mark BMKLocationServiceDelegate

-(void)willStartLocatingUser{

NSLog(@"%s",__FUNCTION__);

}

-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{

NSLog(@"%s",__FUNCTION__);

//1.显示用户位置

mapView.showsUserLocation=YES;

//mapView.centerCoordinate=userLocation.location.coordinate;

//2.更新用户最新位置到地图上

[mapView
updateLocationData:userLocation];

NSLog(@"userLocation==%@",userLocation);

}

/**

*用户方向更新后,会调用此函数

*@param userLocation
新的用户位置

*/

-(void)didUpdateUserHeading:(BMKUserLocation *)userLocation{

NSLog(@"%s",__FUNCTION__);

//mapView.showsUserLocation=YES;

[mapView
updateLocationData:userLocation];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: