[iOS]獲取地理位置信息


1.在工程的 info.plist 文件中增加兩個key( 右鍵 - Add Row )

Privacy - Location Always and When In Use Usage Description

Privacy - Location When In Use Usage Description

value里輸入征求獲取位置信息時展示的提示語:

2.在需要獲取地理位置信息的文件中 

#import <CoreLocation/CoreLocation.h>

在interfce行添加相關的delegate 

CLLocationManagerDelegate

3.在interface構造兩個屬性:

@property (nonatomic,strong) CLLocationManager *locationManager;         //定位
@property (nonatomic,strong) CLGeocoder *geoCoder;                       //地理位置信息

 

4.初始化兩個定位相關的工具類,我們把它寫成一個 名叫 initLocationInfo 的函數:

*此處有個坑爹的地方,定位的獲取必須在主線程中進行,不然的話雖然也會有彈窗提示看起來一切正常,但是不會觸發delegate回調

- (void)initLocationInfo {
    // 初始化定位管理器
    _locationManager = [[CLLocationManager alloc] init];
    // 設置代理
    _locationManager.delegate = self;
    // 設置定位精確度到米
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // 設置過濾器為無
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    // 取得定位權限,有兩個方法,取決於你的定位使用情況
    // 一個是requestAlwaysAuthorization,一個是requestWhenInUseAuthorization
    // 這句話ios8以上版本使用。
    [_locationManager requestAlwaysAuthorization];
    // 開始定位
    [_locationManager startUpdatingLocation];
    //地理信息
    _geoCoder = [[CLGeocoder alloc] init];
}

5.更新的地理位置信息會回調一個delegate:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations

在回調的delegate中我們對回傳的地理信息進行處理:

//MARK: - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    NSLog(@"%lu",(unsigned long)locations.count);
    CLLocation * location = locations.lastObject;
    // 緯度
    CLLocationDegrees latitude = location.coordinate.latitude;
    // 經度
    CLLocationDegrees longitude = location.coordinate.longitude;
    NSLog(@"%@",[NSString stringWithFormat:@"%lf", location.coordinate.longitude]);
    //    NSLog(@"經度:%f,緯度:%f,海拔:%f,航向:%f,行走速度:%f", location.coordinate.longitude, location.coordinate.latitude,location.altitude,location.course,location.speed);
    
    [_geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count > 0) {
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            NSLog(@"%@",placemark.name);
            //獲取城市
            NSString *city = placemark.locality;
            if (!city) {
                //四大直轄市的城市信息無法通過locality獲得,只能通過獲取省份的方法來獲得(如果city為空,則可知為直轄市)
                city = placemark.administrativeArea;
            }
            // 位置名
            NSLog(@"name,%@",placemark.name);
            // 街道
            NSLog(@"thoroughfare,%@",placemark.thoroughfare);
            // 子街道
            NSLog(@"subThoroughfare,%@",placemark.subThoroughfare);
            //
            NSLog(@"locality,%@",placemark.locality);
            //
            NSLog(@"subLocality,%@",placemark.subLocality);
            // 國家
            NSLog(@"country,%@",placemark.country);
        }else if (error == nil && [placemarks count] == 0) {
            NSLog(@"No results were returned.");
        } else if (error != nil){
            NSLog(@"An error occurred = %@", error);
        }
    }];
    //    [manager stopUpdatingLocation];不用的時候關閉更新位置服務,不關閉的話這個 delegate 隔一定的時間間隔就會有回調
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM