學(xué)習(xí)隨記 - 定位服務(wù)

1. IOS定位服務(wù)的開啟與基本設(shè)置

1. 要想使用IOS中的定位服務(wù)首先需要包含頭文件CoreLocation/CoreLocation.h,在interface中聲明一個屬性locationManager

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController () <CLLocationManagerDelegate>

// 定位管理器
@property (strong, nonatomic) CLLocationManager * locationManager;

@end

2. 懶加載

@implementation ViewController

- (CLLocationManager *)locationManager {

    if (!_locationManager) {
        // 實例化管理器
        _locationManager = [[CLLocationManager alloc] init];
        // 設(shè)置管理器類型為普通
        _locationManager.activityType = CLActivityTypeOther;
        // 設(shè)置精度為最高
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        // 設(shè)置自動過濾值
        _locationManager.distanceFilter = 10;
        // 設(shè)置代理
        // 協(xié)議中的方法和定位結(jié)果有關(guān)
        _locationManager.delegate = self;
    }
    return _locationManager;
}

@end

2. 檢測應(yīng)用程序的授權(quán)狀態(tài)

// 獲取定位服務(wù)授權(quán)狀態(tài)
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    
    // 判斷是否已經(jīng)授權(quán)
    if (status == kCLAuthorizationStatusNotDetermined) {
        NSLog(@"未授權(quán)狀態(tài)");
        // 當(dāng)使用應(yīng)用程序時使用定位服務(wù)
        [self.locationManager requestWhenInUseAuthorization];
    } else if (status == kCLAuthorizationStatusDenied) {
        NSLog(@"授權(quán)被拒絕");
        // 創(chuàng)建一個模態(tài)警告視圖控制器
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"溫馨提示" message:@"親,你還沒有開啟定位服務(wù),現(xiàn)在設(shè)置好嗎?" preferredStyle:UIAlertControllerStyleActionSheet];
        // 打開設(shè)置的按鈕
        UIAlertAction *openAction = [UIAlertAction actionWithTitle:@"打開設(shè)置" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            // 點擊設(shè)置按鈕的回調(diào)方法
            // 打開應(yīng)用程序的設(shè)置
            NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            // 通過UIApplication對象的openURL方法可以
            // 打電話 - tel://號碼
            // 發(fā)短信 - sms://號碼
            // 打開網(wǎng)頁 - http(s)://網(wǎng)址
            // 打開App Store - itms-apps://應(yīng)用地址
            // NSURL *url2 = [NSURL URLWithString:@"tel://1008611"];
            // [[UIApplication sharedApplication] openURL:url2];]
            [[UIApplication sharedApplication] openURL:url];
        }];
        // 取消按鈕
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        // 將兩個UIAlertAction(相當(dāng)于兩顆按鈕)加到視圖控制器上
        [alertController addAction:openAction];
        [alertController addAction:cancelAction];
        // 以模態(tài)的方式顯示警告視圖控制器
        [self presentViewController:alertController animated:YES completion:nil];
    }

3. 開始定位

// 開始定位
[self.locationManager startUpdatingLocation];
// 通過協(xié)議方法獲取定位信息
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {

    // locations是獲取的位置信息集合
    NSLog(@"%@", locations.firstObject);
}

4. 范圍監(jiān)測

#pragma mark - 監(jiān)測區(qū)域
- (void) monitorRange {

    // 監(jiān)測中心
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(30.000000, 104.000000);
    // 創(chuàng)建監(jiān)測范圍對象
    // 參數(shù)1:監(jiān)測中心
    // 參數(shù)2:監(jiān)測半徑
    // 參數(shù)3:標(biāo)識
    CLCircularRegion * region = [[CLCircularRegion alloc] initWithCenter:center radius:200 identifier:@"home"];
    // 開始檢測目標(biāo)范圍
    [self.locationManager startMonitoringForRegion:region];
}

#pragma mark - CLLocationManagerDelegate
// 進入監(jiān)測范圍
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {

    NSLog(@"你已進入監(jiān)測區(qū)域");
}

// 離開監(jiān)測范圍
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {

    NSLog(@"你已離開了監(jiān)測區(qū)域");
}

5. 地理編碼和反編碼

#import "ZFGeocoder.h"
#import <CoreLocation/CoreLocation.h>

@implementation ZFGeocoder

// 通過類方法創(chuàng)建一個地理編碼管理器
+ (instancetype) createGeocoder {

    ZFGeocoder * geocoder = nil;
    if (!geocoder) {
        geocoder = [[ZFGeocoder alloc] init];
    }
    return geocoder;
}

// 將地址編碼編成對應(yīng)的經(jīng)緯度
+ (void)getCoordinateWithAddress:(NSString *)address didFinished:(void (^)(CLLocationCoordinate2D))coordinate {

    //將地址編碼成對應(yīng)的經(jīng)緯度
    [[self createGeocoder] geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        //返回值
        CLLocationCoordinate2D tcoordinate = CLLocationCoordinate2DMake(0, 0);
        
        for (CLPlacemark * mark in placemarks) {
            
            //拿到經(jīng)緯度
            tcoordinate = mark.location.coordinate;
        }
        
        //調(diào)用block傳值
        coordinate(tcoordinate);
    }];
}

// 將經(jīng)緯度進行反編碼
+ (void)getAddressWithCoordinate:(CLLocationCoordinate2D)coordinate didFinished:(void (^)(NSDictionary *))address {

    //拿到解析器
    CLGeocoder * geo = [self createGeocoder];
    
    //反編碼
    CLLocation * location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    [geo reverseGeocodeLocation:location completionHandler:^(NSArray *  placemarks, NSError *  error) {
        
        NSDictionary * dict;
        
        //參數(shù)1:反編碼后的結(jié)果
        for (CLPlacemark * mark  in placemarks) {
            //拿到返回值
            dict = mark.addressDictionary;
            //            NSLog(@"%@", mark.addressDictionary);
            //            //獲取詳細信息
            //            NSLog(@"%@", mark.addressDictionary[@"FormattedAddressLines"][0]);
            //            //國家
            //            NSLog(@"國家:%@",mark.addressDictionary[@"Country"]);
            //
            //            //街道:
            //            NSLog(@"街道:%@", mark.addressDictionary[@"Street"]);
        }

        //調(diào)用block傳值
        address(dict);
    }];
}

@end
最后編輯于
?著作權(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,075評論 4 61
  • 出自http://my.oschina.net/are1OfBlog/blog/420034 摘要 現(xiàn)在很多社交、...
    JJO閱讀 4,306評論 4 19
  • 雖然在各方的壓力下,這學(xué)是不得不上了,但是我基本上就是應(yīng)付、混日子,沒有上進心,學(xué)習(xí)成績也不突出,這樣的狀態(tài)維持了...
    雁過留書閱讀 435評論 0 5
  • 整體搭配偏英倫格子風(fēng),熒光綠T恤搭配藍色格子背帶褲另外加了黑白大格子小外套。同一個世界,同一個夢想,同一個套路!熒...
    Sunny劉春秀閱讀 245評論 6 0
  • 早安 4/21 追隨者,永不領(lǐng)先 艾力這篇里提到馬東。 用馬東的事情來陳述不斷更新自己的意義....... 我,更...
    MissC和LadyMewo閱讀 161評論 0 0

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