定位的使用实例

来源:互联网 发布:linux anaconda使用 编辑:程序博客网 时间:2024/05/18 02:39


#import "ViewController.h"


//使用定位之前导入CoreLocation框架

#import <CoreLocation/CoreLocation.h>


@interface ViewController ()<CLLocationManagerDelegate>

{

    //定位使用的类CLLocationManager

    CLLocationManager *manager;

    

}

@end



/**

 *  

 1.LBS:Location Based Service (基于定位的服务)位置服务

 2.苹果提供框架:CoreLocation框架进行定位,可以独立使用;CoreLocation中主要包含了定位,地理编码,反地理编码功能;

 3.地理编码:通过地理位置的字符串,得到关于位置的相关信息(经度,维度,海拔)

 

   反地理编码:通过经纬度得到字符串具体位置的地名

 

 4.CLCoreLocation这个类里面封装了位置和高度信息

 

 5.本初子午线:穿过英国伦敦格林文治天文台;东西经各180,共计360,东经正数,西经负数;赤道为零度纬度,南北纬各90,共计180

 

 6.iOS代码中使用CLLocationCoordinate2D来表示经纬度

 typedef struct {

CLLocationDegrees latitude; 纬度

CLLocationDegrees longitude; 经度

 } CLLocationCoordinate2D;

 

 

 

 7.使用定位之前,判断用户是否打开了定位服务 locationServicesEnabled;向用户说明为什么使用定位(苹果会自动弹出一个弹出框,提示用户需要使用定位服务(第一次使用软件的时候));

 

   iOS8以下,由于位置信息是用户的隐私  需要用户去授权 系统会自动弹出一个对话框让用户授权

 Info.plist中设置NSLocationUsageDescription说明定位的目的(Privacy - Location Usage Description)key 写为什么让用户 打开定位

 

 ios8使用定位需在pilst里面添加NSLocationWhenInUseUsageDescription

 NSLocationAlwaysUsageDescription

 获取用户的授权

 

 

 */

@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    //用户是否打开了定位服务

    BOOL isOpen = [CLLocationManager locationServicesEnabled];

  

    if ( !isOpen ) {

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"请再设置中打开定位服务" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [alert show];

        return ;

    }

    

    

    //初始化定位的服务对象

    manager = [[CLLocationManager alloc]init];

    

    //infoPlist里面设置key value的时候一定要与代码里面一致

    //一直使用定位功能

    //[manager requestAlwaysAuthorization];

    

    //当使用时启用定位服务

    [manager requestWhenInUseAuthorization];

    

//使用定位服务,耗电量大;使用完定位服务,需要关闭定位服务(节省电量)

  

// 让系统帮助管理定位的开关

    manager.pausesLocationUpdatesAutomatically = YES;

//设置定位服务的精准度,越精准越耗电

//1.GPS 2.WIFI 3.蜂窝数据网罗络,被苹果封装到了底层

    

    /*extern const CLLocationAccuracy kCLLocationAccuracyBest;

    extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;

    extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;

    extern const CLLocationAccuracy kCLLocationAccuracyKilometer;

    extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;*/

 

    manager.desiredAccuracy =  kCLLocationAccuracyBest;

    

    //多少米去更新一次位置Location

    manager.distanceFilter = 50;//以自身位置为中心,走出设置的范围就会更新Location

    

    //当获取到位置或者更新位置的位置会调用代理位置

    manager.delegate = self;

}


#pragma mark---代理方法

- (void)locationManager:(CLLocationManager *)manager

     didUpdateLocations:(NSArray *)locations

{

    //locations包含了咱们更新的位置;通过获取数组的最后一个数组元素得到新位置

    NSLog(@"%@",locations);

    CLLocation *curLocation = [locations lastObject];

    

//通过Location获得当前位置的经纬度

    CLLocationCoordinate2D curCoordinate2D = curLocation.coordinate;

//    curCoordinate2D.latitude纬度

//    curCoordinate2D.longitude经度


    

    NSLog(@"经度:%f 纬度:%f",curCoordinate2D.longitude,curCoordinate2D.latitude);

    

    //通过Location获得更新位置的时间(NSdate

    NSDate *updateTime = curLocation.timestamp;

    NSLog(@"%@",updateTime);

    

    //获得当前的速度

    double speed = curLocation.speed;

    NSLog(@"当前行驶速度:%f m/s",speed);

    

    //在室内可以获取在第几楼层(ios8.0以上)

    CLFloor *curFloor = curLocation.floor;

    NSLog(@"@%ld",(long)curFloor.level);

    

    //获得航向 course当前设备前进的方向。该值为表示向北,90°表示向东,180°表示向南,270°表示向西

    CLLocationDirection curDiraction = curLocation.course;

    NSLog(@"%f",curDiraction);

    

    //获得精准度;获得海拔的精准度verticalAccuracy(负数时不能获取海拔精准度)

    CLLocationAccuracy curAccuracy = curLocation.verticalAccuracy;

    NSLog(@"%f",curAccuracy);

    

    //获得海拔高度

    double altitude = curLocation.altitude;

    NSLog(@"海拔:%f",altitude);

    

    //获得行驶的平均速度;需要得到两次跟新的

}


- (void)locationManager:(CLLocationManager *)manager

       didFailWithError:(NSError *)error

{

    NSLog(@"%@",error);

}



- (void)viewWillAppear:(BOOL)animated

{

    //开启定位

    [manager startUpdatingLocation];

    

}



- (void)viewWillDisappear:(BOOL)animated

{

    //关闭定位

    [manager stopUpdatingLocation];

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


0 0
原创粉丝点击