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];
}
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];
}
相关文章推荐
- ios 生成随机数
- iOS 中常用的遍历方法
- ios学习之pickerview的使用
- [iOS]ARC和MRC下的单例
- IOS学习笔记 - NSFileManager,沙盒路径,NSString类路径处理,NSCache,读取文件头判断图片类型
- iOS 键盘通知 与 键盘上方工具条设置
- IOS 关于layoutSubviews摘记
- IOS9 适配
- iOS编程之Address Book (1)
- iOS Document Interaction 编程指南
- ios学习之创建上拉菜单的实现
- iOS开发注意⚠ 持续更新
- ios学习之alertView的基本使用
- iOS中通知中心NSNotificationCenter的应用
- ios NSURLSession使用说明及后台工作流程分析
- iOS 新浪微博-4.0 OAuth授权
- ios开机启动引导页
- 【黑马训练营】IOS C语言代码块
- iOS开发控件基础
- 三种方式使得iOS应用能够在后台进行数据更新和下载