ios中關於系統定位CLLocationManager的使用解析


   //1、添加定位管理委托協議 CLLocationManagerDelegate

 //2、初始化定位管理對象

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

 

    self.locationManager.delegate=self;

    //定位精度

    self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;

    //多長距離更新一次位置

    self.locationManager.distanceFilter=50;

 

    if (UIDevice.currentDevice.systemVersion.integerValue>8.0)

    {

        [self.locationManager requestAlwaysAuthorization];

        [self.locationManager requestWhenInUseAuthorization];

    }

 //開啟定位服務

    if (self.locationManager.locationServicesEnabled) {

        [self.locationManager startUpdatingLocation];

    }

 //3、調用系統定位方法

  

#pragma mark - CLLocation Delegate

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

{

    switch (status)

    {

        case kCLAuthorizationStatusNotDetermined:

            if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])

            {

                [_locationManager requestAlwaysAuthorization];

            }

            break;

        default:

            break;

    }

}

 

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

{

    //獲取當前位置信息

    CLLocation *location = [locations objectAtIndex:0];

    //判斷是否是在國內

    if (![WGS84TOGCJ02 isLocationOutOfChina:[location coordinate]])

    {

   //設置鎖,防止並發寫入

        [[NSUserDefaults standardUserDefaults] synchronize];

        

        //轉換后的coord

        CLLocationCoordinate2D coord = [WGS84TOGCJ02 transformFromWGSToGCJ:[location coordinate]];

        _myCoordinate = coord;

        [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%f",_myCoordinate.latitude] forKey:KCurrentLat];

        [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%f",_myCoordinate.longitude] forKey:KCurrentLng];

        

    }

    

    //創建反地理編碼對象

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

 //將經緯度信息轉換成字符串位置信息

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *array, NSError *error)

     {

//         NSLog(@"placemark:%@",array);

         if (array.count > 0)

         {

             CLPlacemark *placemark = [array objectAtIndex:0];

             if (![DGFunction xfunc_check_strEmpty:placemark.locality])

             {

                 [[ NSUserDefaults standardUserDefaults] setObject:placemark.locality forKey:@"localCity"];

                 [[NSUserDefaults standardUserDefaults] synchronize];

//                 NSDictionary *dic = @{@"city":placemark.locality};

                 

                 if (![DGFunction xfunc_check_strEmpty:placemark.locality]) {

                     [[NSUserDefaults standardUserDefaults] setObject:placemark.administrativeArea forKey:k_Current_Province];

                 }

                 

                 if (![DGFunction xfunc_check_strEmpty:placemark.locality]) {

                     [[NSUserDefaults standardUserDefaults] setObject:placemark.locality forKey:k_Current_City];

                 }

                 

                 if (![DGFunction xfunc_check_strEmpty:placemark.subLocality]) {

                     [[NSUserDefaults standardUserDefaults] setObject:placemark.subLocality forKey:k_Current_Area];

                 }

                 

                 [[NSUserDefaults standardUserDefaults] synchronize];

                 NSLog(@"locality:%@ subLocality:%@",placemark.locality,placemark.subLocality);

                 //關閉定位服務

      [_locationManager stopUpdatingLocation];

             }

         }

     }];

}


免責聲明!

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



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