标签:
一、LBS(定位)的使用
1、使用框架Core Location
2、CLLocationManager
(1)CoreLocation中使用CLLocationManager对象来做用户定位
(2)CLLocationManager的常用操作
<1>开始用户定位
- (void)startUpdatingLocation;
<2>停止用户定位
- (void) stopUpdatingLocation;
<3>当调用了startUpdatingLocation方法后,就开始不断地定位用户的位置,中途会频繁地调用代理的下面方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
locations参数里面装着CLLocation对象
<4> 判断当前定位是否可用,最好在使用前判断
+locationServicesEnabled
(3)定位的精确度
<1>@property(assign, nonatomic) CLLocationDistance distanceFilter;
每隔多少米定位一次
<2>@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;
定位精确度(越精确就越耗电)
3、CLLocation
(1)CLLocation对象用来表示某个位置的地理信息,比如经纬度、海拔等等
(2)经纬度
@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;
(3)海拔
@property(readonly, nonatomic) CLLocationDistance altitude;
(4)路线,航向(取值范围是0.0° ~ 359.9°,0.0°代表真北方向)
@property(readonly, nonatomic) CLLocationDirection course;
(5)行走速度(单位是m/s)
@property(readonly, nonatomic) CLLocationSpeed speed;
(6)计算两个位置间的距离
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
4、CLLocationCoordinate2D
(1)CLLocationCoordinate2D是一个用来表示经纬度的结构体
(2)定义如下:
typedef struct {
CLLocationDegrees latitude; // 纬度
CLLocationDegrees longitude; // 经度
} CLLocationCoordinate2D;
(3)一般用CLLocationCoordinate2DMake函数来创建CLLocationCoordinate2D
5、关于用户隐私
(1)从iOS 6开始,苹果在保护用户隐私方面做了很大的加强,很多操作都必须经过用户批准授权:
<1>获得用户的位置
<2>访问用户的通讯录
<3>日历
<4>相机
<5>相册
<6>等等
(2)如何设置用户隐私
<1>开发者可以在Info.plist中设置NSLocationUsageDescription说明定位的目的(Privacy - Location Usage Description)
<2> 总是使用用户位置:NSLocationAlwaysUsageDescription
使用应用时定位:NSLocationWhenInUseDescription
<3> 不设置该字段,定位不能使用(至少设置其中之一,两个都设置,NSLocationAlwaysUsageDescription生效)
6、示例代码
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewController ()<CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager *manager; @end @implementation ViewController - (CLLocationManager *)manager { if (!_manager) { //1.创建一个定位管理器 _manager = [[CLLocationManager alloc]init]; } return _manager; } - (void)viewDidLoad { [super viewDidLoad]; //2.获取用户的授权 [self.manager requestAlwaysAuthorization]; //3.开始定位 [self.manager startUpdatingLocation]; //4.设置代理 self.manager.delegate = self; //设置属性 /* //导航专用的精确度 extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0); extern const CLLocationAccuracy kCLLocationAccuracyBest; extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters; extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters; extern const CLLocationAccuracy kCLLocationAccuracyKilometer; extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers; */ //定位精确度 self.manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; //使用场景 //求距离 CLLocation *loc1 = [[CLLocation alloc]initWithLatitude:40 longitude:116]; CLLocation *loc2 = [[CLLocation alloc]initWithLatitude:41 longitude:116]; CLLocationDistance dis = [loc1 distanceFromLocation:loc2]; NSLog(@"%f",dis); } #pragma mark - CLLocationManagerDelegate - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { //1.获取用户的位置 CLLocation *location = [locations firstObject]; CLLocationCoordinate2D coordinate = location.coordinate; NSLog(@"%f ---- %f",coordinate.latitude,coordinate.longitude); NSLog(@"locations"); } @end
标签:
原文地址:http://www.cnblogs.com/cleven/p/5432213.html