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

CoreLocation定位服务

2015-08-04 14:38 495 查看
一、简单说明



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文件中添加NSLocationWhenInUseUsageDescription,value可以不写

        //授权
        [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];

   

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