IOS 定位 单例

+ (SCLocationController *)sharedController
{
     static SCLocationController *sharedController = nil;
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
        sharedController = [[self alloc]init];
  });

  return sharedController;
}

- (id)init
{
     self = [super init];
     if (self) {
        _locationManager = [[CLLocationManager alloc]init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.distanceFilter = 30; // Meters.
  }
  return self;
}

#pragma mark - Location Manager
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    [_locManager stopUpdatingLocation]; 

    CLGeocoder *reverseGeocoder=[[CLGeocoder alloc]  init];
    [reverseGeocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error)
     {
         CLPlacemark *placeMark = [array lastObject];
         if (placeMark != nil)
         {
             state = [placeMark.addressDictionary objectForKey:@"State"];  
             city = [placeMark.addressDictionary objectForKey:@"City"];                   
             subLocality = [placeMark.addressDictionary objectForKey:@"SubLocality"]; 
             
             if (city.length >  0) {
                 NSLog(@"%@-%@-%@",state,city,subLocality);
             }
             else{
                 NSLog(@"%@-%@",state,subLocality);
             }
             
         }
 
     }];
}

  

posted @ 2014-04-29 10:16  菜鸟程序猿  阅读(342)  评论(0编辑  收藏  举报