前提:在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;
-(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];
}