iOS開發首次進入需要獲取地理位置


今天給大家簡單介紹一下iOS開發過程中會遇到的獲取地理位置的問題,(話不多說進入正題)這里給大家講解一下兩種在APPdelegate獲取地理位置的方法:

一:首先是用系統的方法獲取地理位置:

     1、 首先在AppDelegate.m導入

           @import CoreLocation;

           @import MapKit;

    2、 聲明協議:

           <CLLocationManagerDelegate> //協議

    3、聲明屬性:

           @property(nonatomic) CLLocationManager *locationManager;

    4、

          - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

               self.locationManager = [[CLLocationManager alloc] init];

               //判斷異常

               if (![CLLocationManager locationServicesEnabled]) {

               NSLog(@"定位服務不可用!");

                }

           //定位精度

              _locationManager.desiredAccuracy = kCLLocationAccuracyBest;

              self.locationManager.delegate = self;

           //啟動定位

              [_locationManager startUpdatingLocation];

               ....

            return YES;

       }

     5、完成地圖的代理方法:

        #pragma mark -- CLLocationManagerDelegate

 

       - (void)locationManager:(CLLocationManager *)manager

           didUpdateLocations:(NSArray<CLLocation *> *)locations {

           static BOOL isLocation = NO;

            //地理位置逆編碼

           [self veverseGeLocation:manager.location completion:^(BOOL sucess, id content) {

            if (sucess) {

            //這里打印一下數據,方便查看

                NSLog(@"%@",content);

            //NSString * provnice = content[@"State"];

            //NSString * city = content[@"City"];

            //NSString * district = content[@"SubLocality"];

    

            if (isLocation == YES) {

                return ;

            }

        //如果你其他界面需要用到該地理位置;則直接將NSString 內容保存下來,需要在取出來

            isLocation = YES;

            [self.locationManager stopUpdatingLocation];

        }else{

            

          }

        }];

         //停止定位

          [manager stopUpdatingLocation];

         }

         //地理位置逆編碼:把經緯度信息編碼成格式化的地理位置信息

       - (void)veverseGeLocation:(CLLocation *)location completion:(void(^)(BOOL sucess, id content)) completion{

           CLGeocoder *coder = [[CLGeocoder alloc]init];

         //逆編碼方法,后台線程執行

          [coder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

            BOOL sucess;

            id content = nil ;

            if (error) {

            sucess = NO;

            content = error.localizedDescription;

           }

          else{

            sucess = YES;

            CLPlacemark *placeMark = placemarks.firstObject;

            content = placeMark.addressDictionary;            

        }

        completion(sucess,content);      

        }]; 

    }

 二:首先是用高德的方法獲取地理位置:用高德地圖的話首先需要添加開發地圖需要用到的庫:對於庫的導入依據開發人員的實際需求而定進行cocoapods導入或者手動拖入,前面流程基本都是跟隨高德地圖api 進行的,再次就不多重復介紹,詳情各位可自行前往學習(鏈接:http://lbs.amap.com/api/ios-sdk/guide/create-project,下面就APPdelegate的內容進行講解:

1、導入對應的頭文件:

     #import <AMapFoundationKit/AMapFoundationKit.h>

     #import <AMapLocationKit/AMapLocationKit.h>

2、添加協議:

     <AMapLocationManagerDelegate> //協議

3、聲明屬性:

     @property (nonatomic,strong)AMapLocationManager * locationManager;

4、

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

 

    [AMapServices sharedServices].apiKey = @"您的key";

    [[AMapServices sharedServices] setEnableHTTPS:YES];

    

 

   //創建地圖manager

   self.locationManager = [[AMapLocationManager alloc] init];//實例化位置管理器

    [self.locationManager setDelegate:self];

    [self.locationManager startUpdatingLocation];//啟動定位

    [self.locationManager setPausesLocationUpdatesAutomatically:NO];

    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];//定位精度

    self.locationManager.locatingWithReGeocode = YES;//連續定位是否返回逆地理信息,默認NO。

    

    return YES;

   }

5、代理方法:

     

   #pragma mark - AMapLocationManager Delegate

   - (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode       *)reGeocode

   {

    NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);

        static BOOL isLocation = NO;

     if (reGeocode != nil && isLocation == NO) {

      //  NSString * provnice = reGeocode.province;

      //  NSString * city = reGeocode.city;

      //  NSString * district = reGeocode.district;

      //  NSString *codeStr = reGeocode.adcode;//地理adcode

      //如果你其他界面需要用到該地理位置;則直接將NSString 內容保存下來,需要在取出來

        isLocation = YES;

       }

   }

 以上就是兩者獲取地理位置的方法,需要說明的是:如果開發項目中后面會用到地理位置的adcode的時候,本人建議用第二種方法,因為第一種目前不能獲取到該adcode,或者本人技術不到位還沒有找到解決的辦法,望各位大神多多諒解和給出指導意見,謝謝!

 


免責聲明!

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



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