iOS自帶定位的使用

配置:

  1. info.plist中配置Privacy - Location When In Use Usage Description
  2. 導(dǎo)入庫(kù)文件#import <CoreLocation/CoreLocation.h>

代碼:

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

@interface RootViewController ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;

@end

#implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self startLocation];
}

// 開啟定位
- (void)startLocation {
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;//最精準(zhǔn)
    
    /** 由于IOS8中定位的授權(quán)機(jī)制改變 需要進(jìn)行手動(dòng)授權(quán)
     * 獲取授權(quán)認(rèn)證,兩個(gè)方法:
     * [self.locationManager requestWhenInUseAuthorization];
     * [self.locationManager requestAlwaysAuthorization];
     */
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        NSLog(@"requestWhenInUseAuthorization");
        //  [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager requestAlwaysAuthorization];
    }
    // 開始定位,不斷調(diào)用其代理方法
    [self.locationManager startUpdatingLocation];
}

#pragma mark  【 CLLocationManagerDelegate 】
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
    // 1.獲取用戶位置的對(duì)象
    CLLocation *location = [locations lastObject];
    CLLocationCoordinate2D coordinate = location.coordinate;
    NSLog(@"緯度:%f 經(jīng)度:%f", coordinate.latitude, coordinate.longitude);
    longitute = coordinate.longitude;
    latitude = coordinate.latitude;
    
    // 獲取當(dāng)前所在的城市名
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //根據(jù)經(jīng)緯度反向地理編譯出地址信息
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
     {
         for (CLPlacemark * placemark in placemarks) {
             
             NSString *city = placemark.locality;
             if (!city) {
                 //四大直轄市的城市信息無(wú)法通過(guò)locality獲得,只能通過(guò)獲取省份的方法來(lái)獲得(如果city為空,則可知為直轄市)
                 city = placemark.administrativeArea;
             }
             NSDictionary *dictionary = [placemark addressDictionary];
             NSLog(@"國(guó)家:%@",[dictionary objectForKey:@"Country"]);
             NSLog(@"?。?@",[dictionary objectForKey:@"State"]);
             NSLog(@"市:%@",city);
             NSLog(@"區(qū):%@",placemark.subLocality);
             NSLog(@"街道:%@",placemark.thoroughfare);
             NSLog(@"子街道:%@",placemark.subThoroughfare);
         }
         if (error == nil && [placemarks count] == 0) {
             NSLog(@"No results were returned.");
         } else if (error != nil) {
             NSLog(@"An error occurred = %@", error);
         }
     }];
    
    // 2.停止定位
    [manager stopUpdatingLocation];
}

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

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

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