iOS定位的使用

簡介

定位,顧名思義,就是確定你所在的位置。

一、介紹

? 定位使用CoreLocation框架

?功能

    1、基礎(chǔ)定位

    2、地理編碼反編碼  

? iOS8 iOS9之后的改變

     1、定位服務(wù)的目的

     (1)NSLocationAlwaysUsageDescription

     (2)NSLocationWhenInUseUsageDescription

注意:如果忘記寫就不能使用定位功能,沒有提示信息

    2、請求用戶授權(quán)

    (1)requestAlwaysAuthorization

    (2)requestWhenInUseAuthorization

注意:如果和描述的目的不匹配,也不能使用

     3、iOS9 按Home鍵進入后臺,如果需要繼續(xù)定位

    (1)需要在info.plist文件里添加Required background modes->App registers for location updates  如果不添加這對鍵值  卻使用后臺定位功能  會直接崩潰

    (2)allowsBackgroundLocationUpdates  這個屬性需要同時設(shè)置為YES

二、使用


#import "ViewController.h"

#import<CoreLocation/CoreLocation>

@interface ViewController (){

CLLocationManager *locationManager;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

//    判斷用戶是否在設(shè)置里面打開了位置服務(wù)功能

if (![CLLocationManager locationServicesEnabled]) {

//        1、跳彈出框 提示用戶打開步驟

//        2、通過代碼跳到設(shè)置頁面

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"溫馨提示" message:@"請在設(shè)置中打開定位功能" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

[alertController addAction:action];

[self presentViewController:alertController animated:YES completion:nil];

}



//    openURL:用于跳轉(zhuǎn)APP  跳到iOS允許跳到的頁面

//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

//    if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {

//             跳轉(zhuǎn)到設(shè)置頁面

//        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

//    }

//    [self respondsToSelector:@selector(selector)];//判斷方法是否響應(yīng)

//    1、創(chuàng)建管理者的對象

locationManager = [[CLLocationManager alloc]init];

//    多少米,去更新一次位置信息

locationManager.distanceFilter = 100;

//    設(shè)置定位的精準(zhǔn)度

locationManager.desiredAccuracy = 10;

//    2、info中添加描述使用定位的目的 并向用戶申請授權(quán)

[locationManager requestWhenInUseAuthorization];

//    3、 掛上代理  并實現(xiàn)代理方法

locationManager.delegate = self;

//    4、如果需要使用后臺定位服務(wù)需要在info 中添加Required background modes 這個KEY 以及它里邊的元素App registers for location updates

locationManager.allowsBackgroundLocationUpdates = YES;

//    5、開始定位

[locationManager startUpdatingLocation];

}
//定位成功的代理方法
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations{
  NSLog(@"定位成功");
}
//定位失敗的代理方法
- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error{
    NSLog(@"定位失敗");
if (error.code == kCLErrorDenied) {
        
        // 提示用戶出錯原因,可按住Option鍵點擊 KCLErrorDenied的查看更多出錯信息,可打印error.code值查找原因所在
        
    }

}

如果想要獲取定位城市的名字,可在定位成功的代理方法里邊添加如下代碼:

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations{
    NSLog(@"定位成功");
   //此處locations存儲了持續(xù)更新的位置坐標(biāo)值,取最后一個值為最新位置,如果不想讓其持續(xù)更新位置,則在此方法中獲取到一個值之后讓locationManager stopUpdatingLocation
 CLLocation *currentLocation = [locations lastObject];
    
    NSLog(@"locations===%@",[locations lastObject]);

    // 獲取當(dāng)前所在的城市名
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    
    //根據(jù)經(jīng)緯度反向地理編譯出地址信息
    
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error)
     
     {
         if (array.count > 0)
             
         {
             CLPlacemark *placemark = [array objectAtIndex:0];
            NSLog(@"%@",placemark.name);
           
 //獲取城市
             NSString *city = placemark.locality;
             
             if (!city) {
                 //四大直轄市的城市信息無法通過locality獲得,只能通過獲取省份的方法來獲得(如果city為空,則可知為直轄市)
                 city = placemark.administrativeArea; 
             }
             NSLog(@"%@",city);
         }
         else if (error == nil && [array count] == 0)
         {
             NSLog(@"No results were returned.");
         }
         
         else if (error != nil)
         {
            NSLog(@"An error occurred = %@", error);
         }
         
     }];
    
    //系統(tǒng)會一直更新數(shù)據(jù),直到選擇停止更新,因為我們只需要獲得一次經(jīng)緯度即可,所以獲取之后就停止更新
    
    [manager stopUpdatingLocation];
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,426評論 4 61
  • 昨天本來去羊湖,在將要出拉薩城的時候,改道納木錯,今天繼續(xù)羊湖之約…… 司機換了,上車后發(fā)現(xiàn)換了一位年齡稍微小一些...
    眾心無相閱讀 426評論 2 3
  • 忽逢瑞云戲日邊,東來紫氣圣霞嬈。 比翼雙蝶翩翩舞,擎天碧木青枝高。 天闕氤氳蘊仙蓮,瑤池帝鄉(xiāng)龍凰傲。 驕日麒麟駕霧...
    九噬閱讀 332評論 2 4
  • 渲染react元素 在第一個例子中就已經(jīng)接觸過了 首先定義react元素 使用ReactDOM.render方法將...
    yanghanbin_it閱讀 1,403評論 0 0
  • 總是能遇到一些傻x,生活中,工作中都有,他們欺負(fù)你,算計你,人前一套背后一套。你拿誠意對他們,他們還不說個正經(jīng)話,...
    一棵樹林閱讀 313評論 1 1

友情鏈接更多精彩內(nèi)容