定位的使用執行個體,定位使用執行個體

來源:互聯網
上載者:User

定位的使用執行個體,定位使用執行個體


#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當前裝置前進的方向。該值為0°表示向北,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


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.