ios開發之 -- 調用系統定位獲取當前經緯度與地理信息


ios 10定位:

在info.plist中加入:

//允許在前台使用時獲取GPS的描述

定位權限:Privacy - Location When In Use Usage Description 

//允許永久使用GPS描述

定位權限: Privacy - Location Always Usage Description

如下圖:

然后再添加framework包,如下圖:

代碼如下:

1,導入系統文件,代理:

#import <CoreLocation/CoreLocation.h>
@interface MainViewController ()<CLLocationManagerDelegate

2,聲明全局變量

@interface MainViewController ()<CLLocationManagerDelegate>
{
    CLLocationManager *locationmanager;//定位服務
    NSString *currentCity;//當前城市
    NSString *strlatitude;//經度
    NSString *strlongitude;//緯度
}

3,聲明方法:

//獲取經緯度
    [self getLocation];

4,

-(void)getLocation
{
    //判斷定位功能是否打開
    if ([CLLocationManager locationServicesEnabled]) {
        locationmanager = [[CLLocationManager alloc]init];
        locationmanager.delegate = self;
        [locationmanager requestAlwaysAuthorization];
        currentCity = [NSString new];
        [locationmanager requestWhenInUseAuthorization];
        
        //設置尋址精度
        locationmanager.desiredAccuracy = kCLLocationAccuracyBest;
        locationmanager.distanceFilter = 5.0;
        [locationmanager startUpdatingLocation];
    }
}

5,定位失敗后的代理方法

#pragma mark CoreLocation delegate (定位失敗)
//定位失敗后調用此代理方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    //設置提示提醒用戶打開定位服務
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"允許定位提示" message:@"請在設置中打開定位" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"打開定位" style:UIAlertActionStyleDefault handler:nil];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:okAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}

6,定位成功后的代理方法

#pragma mark 定位成功后則執行此代理方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    [locationmanager stopUpdatingHeading];
    //舊址
    CLLocation *currentLocation = [locations lastObject];
    CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
    //打印當前的經度與緯度
    NSLog(@"%f,%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
    
    //反地理編碼
    [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count > 0) {
            CLPlacemark *placeMark = placemarks[0];
            currentCity = placeMark.locality;
            if (!currentCity) {
                currentCity = @"無法定位當前城市";
            }
            
            /*看需求定義一個全局變量來接收賦值*/
            NSLog(@"----%@",placeMark.country);//當前國家
            NSLog(@"%@",currentCity);//當前的城市
//            NSLog(@"%@",placeMark.subLocality);//當前的位置
//            NSLog(@"%@",placeMark.thoroughfare);//當前街道
//            NSLog(@"%@",placeMark.name);//具體地址
            
        }
    }];
    
}

打印如下:

 

定位服務,最好在真機上運行,模擬器也可以,不過很坑,也很不准確!

 


免責聲明!

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



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