CLGeocoder只显示最近的苹果商店?

时间:2012-06-08 06:15:11

标签: ios5 clgeocoder

CLGeocoder现在是那么粗糙吗?我期待一些相对接近街道地址的东西。我在5.1模拟器上测试并使用ARC。如果有帮助的话,我现在做了一个快速测试项目:

    - (IBAction)getLocationPressed {
      if ([CLLocationManager locationServicesEnabled] &&
          [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {    
        [self.geoCoder reverseGeocodeLocation:self.locationManager.location completionHandler: 
         ^(NSArray *placemarks, NSError *error) {         
// Note: there is NO guarantee that the CLGeocodeCompletionHandler will be invoked on the main thread
          dispatch_async(dispatch_get_main_queue(),^ {
            NSLog(@"placemarks count: %d", [placemarks count]);
            CLPlacemark *placemark = [placemarks objectAtIndex:0];  
// Note: if a poor location is specified, there may be multiple placemarks for the given location    
            NSString *currentAddress = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
            NSLog(@"I am currently at %@", currentAddress);    
            self.locationLabel.text = currentAddress;
          });
        }];
      }
    }

    #pragma mark - CLLocationManager Delegate Methods 
    - (void)locationManager:(CLLocationManager *)manager 
        didUpdateToLocation:(CLLocation *)newLocation 
               fromLocation:(CLLocation *)oldLocation {      
      // do something...
    }    
    - (void)locationManager:(CLLocationManager *)manager
           didFailWithError:(NSError *)error {
      if (error.code == kCLErrorDenied) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" 
                                message:@"this can not work without location services enabled" 
                                delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
      }
    }

    #pragma mark - Lifecycle Methods
    - (void)viewDidLoad {
      [super viewDidLoad];
      self.locationManager.delegate = self;
      self.locationManager.purpose = REASON_FOR_USING_LOCATION;
      self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;//kCLLocationAccuracyNearestTenMeters;
      [self.locationManager startUpdatingLocation];
    }

由于

1 个答案:

答案 0 :(得分:0)

YESSSS!事实证明,我只是没有在模拟器中启用位置。它实际上有效,如果我在我的设备上测试它。这是一个完整的.m文件的示例,如果它可以帮助任何人。我已经设置了几个标签和一个圆角矩形按钮,您可以从代码中看到:

#define REASON_FOR_USING_LOCATION  (@"to find the closest widget")

@interface ViewController ()  <CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *locationLabel;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLGeocoder *geoCoder;
@end

@implementation ViewController
#pragma mark - Getters/Setters
@synthesize locationLabel = _locationLabel;
@synthesize locationManager = _locationManager;
@synthesize geoCoder = _geoCoder;
// lazily instantiate as required
- (CLGeocoder *)geoCoder {
  if (!_geoCoder) _geoCoder = [CLGeocoder new];
  return _geoCoder;
}

- (CLLocationManager *)locationManager {
  if (!_locationManager) _locationManager = [CLLocationManager new];
  return _locationManager;
}

#pragma mark - Target/Action Methods
- (IBAction)clearLocationPressed {
  self.locationLabel.text = @""; 
}


- (IBAction)getLocationPressed {
  if ([CLLocationManager locationServicesEnabled] &&
      [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {

    [self.geoCoder reverseGeocodeLocation:self.locationManager.location completionHandler: 
     ^(NSArray *placemarks, NSError *error) {

      // Note: there is NO guarantee that the CLGeocodeCompletionHandler will be invoked on the main thread
      dispatch_async(dispatch_get_main_queue(),^ {
        NSLog(@"placemarks count: %d", [placemarks count]);
        CLPlacemark *placemark = [placemarks objectAtIndex:0];  
        // Note: if a poor location is specified, there may be multiple placemarks for the given location

        NSString *currentAddress = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];

        NSLog(@"I am currently at %@", currentAddress);

        self.locationLabel.text = currentAddress;
      });
    }];
  }
}

#pragma mark - CLLocationManager Delegate Methods 
- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
           fromLocation:(CLLocation *)oldLocation {      
  // do something...
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {
  if (error.code == kCLErrorDenied) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" 
                            message:@"this can not work without location services enabled" 
                            delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
  }
}

#pragma mark - Lifecycle Methods
- (void)viewDidLoad {
  [super viewDidLoad];
  self.locationManager.delegate = self;
  self.locationManager.purpose = REASON_FOR_USING_LOCATION;
  self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;//kCLLocationAccuracyNearestTenMeters;
  [self.locationManager startUpdatingLocation];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  return YES;
}

- (void)viewDidUnload {
  [self setLocationLabel:nil];
  [self setLocationManager:nil];
  [self setGeoCoder:nil];
  [super viewDidUnload];
}
@end