CoreLocation定位服务


一、简单说明

1.CLLocationManager

CLLocationManager的常用操作和属性

开始用户定位­ (void)startUpdatingLocation;

停止用户定位­ (void) stopUpdatingLocation;

说明:当调用了startUpdatingLocation方法后,就开始不断地定位用户的位置,中途会频繁地调用代理的下 面方法

­ (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;


每隔多少米定位一次
@property(assign, nonatomic) CLLocationDistance distanceFilter; 定位精确度(越精确就越耗电)
@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;

//判断用户定位服务是否开启
if ([CLLocationManager locationServicesEnabled]

//计算两个经纬度之间的直线距离
//根据经纬度创建两个位置对象
CLLocation *loc1=[[CLLocation alloc]initWithLatitude:40 longitude:116];
CLLocation *loc2=[[CLLocation alloc]initWithLatitude:41 longitude:116];
CLLocationDistance distance=[loc1 distanceFromLocation:loc2];


2.CLLocation
CLLocation 用来表示某个位置的地理信息,比如经纬度、海拔等等
(
1 )经纬度
@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;

( 2 )海拔
@property(readonly, nonatomic) CLLocationDistance altitude;

( 3 )路线,航向(取值范围是 0.0° ~ 359.9° , 0.0° 代表真北方向)  
@property(readonly, nonatomic) CLLocationDirection course;

( 4 )行走速度(单位是 m/s )
@property(readonly, nonatomic) CLLocationSpeed speed;

( 5 )计算 2 个位置之间的距离
­ (CLLocationDistance)distanceFromLocation:(const CLLocation *)location 方法


3.CLLocationCoordinate2D

CLLocationCoordinate2D是一个用来表示经纬度的结构体,定义如下 typedef struct {

CLLocationDegrees latitude; // 纬度

CLLocationDegrees longitude; // 经度 } CLLocationCoordinate2D;


一般用 CLLocationCoordinate2DMake 函数来创建 CLLocationCoordinate2D


4. CLLocationManagerDelegate
/* *
  *  当定位到用户的位置时,就会调用(调用的频率比较频繁)
  */
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
      //locations 数组里边存放的是 CLLocation 对象,一个 CLLocation 对象就代表着一个位置
   
CLLocation *loc = [locations firstObject];
   
   
// 维度: loc.coordinate.latitude
   
// 经度: loc.coordinate.longitude
   
NSLog ( @" 纬度 =%f ,经度 =%f" ,loc. coordinate . latitude ,loc. coordinate . longitude );
    NSLog(
@"%d" ,locations.count);
   
   
// 停止更新位置(如果定位服务不需要实时更新的话,那么应该停止位置的更新)
    //    [self.locMgr stopUpdatingLocation]; 

}

注意:不要使用局部变量(创建位置管理器),因为局部变量的方法结束它就被销毁了。建议使用一个全局的变量,且只创建一次就可以了(使用懒加载)。





//-------------------------------- 开启定位 -------------------------------
1.导入框架
#import <CoreLocation/CoreLocation.h>

2.遵守协议
< CLLocationManagerDelegate >

3.定义定位管理者属性,并懒加载
@property ( nonatomic , strong ) CLLocationManager * manager ;

- ( CLLocationManager *) manager
{
#warning 定位服务不可用
    if (![ CLLocationManager locationServicesEnabled ]) return nil ;
   
    if (! _manager ) {
        // 创建定位管理者
         self. manager = [[CLLocationManager alloc] init];
        // 设置代理
        self. manager .delegate = self;
    }
    return _manager ;
} 

4.开始定位

- ( void )viewDidLoad
{   //设置精度
          // 精度越高,耗损的电量越大
    self. manager . desiredAccuracy = kCLLocationAccuracyNearestTenMeters ;
   
   
if ([[ UIDevice currentDevice ]. systemVersion floatValue ] >= 8.0) {
          
        // 在info. plsit 文件中添加 NSLocationAlwaysUsageDescription value 可以不写      
        //[_manager requestAlwaysAuthorization];
        //在info.plsit文件中添加NSLocationWhenInUseUsageDescriptionvalue可以不写

        //授权
        [ self. manager requestAlwaysAuthorization ];
    }
   
   
// 开始定位
    [ self. manager startUpdatingLocation];
}


#pragma mark - CLLocationManager Delegate

// 定位调用的代理方法
- (
void )locationManager:( CLLocationManager *)manager didUpdateLocations:( NSArray *)locations
{
   
   
// 1 )获取经纬度
   
CLLocation *location = [locations lastObject ];
   
CLLocationCoordinate2D coordinate = location. coordinate ;
   
   
// 2 )停止定位
    [manager
stopUpdatingLocation ];
   
}
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值