iOS定位-核心定位框架CLLocation定位經(jīng)緯度、所在城市

原文鏈接:biggergao.github.io/CLLocation

我是前言

最近做了一下CLLocation相關(guān)的東西,較全面的寫了點(diǎn)相關(guān)問題與解決辦法,文章的demo可以在這里下載(有點(diǎn)RAC的知識)。如果是模擬器,運(yùn)行時(shí)請選擇Location GPX文件


祝學(xué)習(xí)愉快。

副本主要任務(wù)

  • 定位設(shè)備經(jīng)緯度與所在城市

預(yù)備知識-CLLocation對象(可跳過)

CLLocation對象存儲著CLLocationManager對象生成的位置數(shù)據(jù),先介紹一下它的屬性大概了解CLLocation是什么東西

用于定位的屬性 含義
coordinate <b>地理位置(經(jīng)緯度)
altitude 海拔
floor 建筑內(nèi)邏輯樓層
timestamp 定位時(shí)間戳
horizontalAccuracy 水平技能范圍,單位米(見注1)
verticalAccuracy 海拔誤差,單位米

注1:我們在地圖上的點(diǎn)由經(jīng)度和緯度確定,horizontalAccuracy表示該圓的半徑是多大(單位為米),<b>負(fù)值表示該點(diǎn)無效(經(jīng)常用在if語句中判斷點(diǎn)是否可用)</b>。

用于速度和方向的屬性 含義
speed 瞬時(shí)速度
course 設(shè)備移動(dòng)方向

實(shí)戰(zhàn)

1.模擬器參數(shù)設(shè)置(可跳過)

1.1添加GPX文件設(shè)置

修改latitude(經(jīng)度)和longitude(緯度)的值,可以使用圖上的值lat="29.568863"和lon="106.460922",美麗山城重慶。

最后調(diào)試選擇對應(yīng)的GPX文件即可

1.2直接修改模擬器的值

修改參數(shù)即可

2.獲取經(jīng)緯度

2.1 iOS8前的BUG

我們需要在info.plist文件里添加兩個(gè)字段給APP定位權(quán)限,不然在iOS8后是無法啟動(dòng)定位的。他們分別是

屬性名 含義
NSLocationWhenInUseUsageDescription 使用期間
NSLocationAlwaysUsageDescription 始終開啟

添加如下:


上個(gè)效果圖好理解點(diǎn):



2.2核心代碼講解

- (void)findCurrentLocation {
    self.isFirstUpdate = YES;
    // 1
    if (! [CLLocationManager locationServicesEnabled]) {
        [TSMessage showNotificationWithTitle:@"未開啟定位服務(wù)"
                                    subtitle:@"請開啟定位服務(wù)定位您所在城市."
                                        type:TSMessageNotificationTypeError];
    }
    // 2
    else if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager startUpdatingLocation];
    }
    // 3
    else {
        [self.locationManager requestAlwaysAuthorization];
        [self.locationManager startUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    if (self.isFirstUpdate) {
        // 4
        self.isFirstUpdate = NO;
        return;
    }
    
    // 5
    CLLocation *newLocation = [locations lastObject];
    
    self.currentLocation = newLocation;
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    // 反向地理編譯出地址信息
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (! error) {
            if ([placemarks count] > 0) {
                CLPlacemark *placemark = [placemarks firstObject];
                
                // 獲取城市
                NSString *city = placemark.locality;
                if (! city) {
                    // 6
                    city = placemark.administrativeArea;
                }
                
                self.currentCity = city;
            } else if ([placemarks count] == 0) {
                [TSMessage showNotificationWithTitle:@"GPS故障"
                                            subtitle:@"定位城市失敗"
                                                type:TSMessageNotificationTypeError];
            }
        } else {
            [TSMessage showNotificationWithTitle:@"網(wǎng)絡(luò)錯(cuò)誤"
                                        subtitle:@"請檢查您的網(wǎng)絡(luò)"
                                            type:TSMessageNotificationTypeError];
        }
    }];
    [self.locationManager stopUpdatingLocation];
}

1、未開啟定位服務(wù)

2、使用時(shí)定位

3、始終定位

4、第一次數(shù)據(jù)可以是久值,需舍棄

5、locations中有兩個(gè)元素,第一個(gè)為舊值,第二個(gè)為新值

6、四大直轄市的城市信息無法通過locality獲得,只能通過獲取省份的方法來獲得(如果city為空,則可知為直轄市)

最后通關(guān)副本:


參考博客:

http://blog.csdn.net/ndscoahz/article/details/42418729

http://blog.it985.com/13173.html

Done

作者 @biggergao

2016年05月15日

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

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

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