0

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];
    }

谢谢

4

1 回答 1

0

是的!事实证明,我只是没有在模拟器中启用位置。如果我在我的设备上测试它,它实际上是有效的。这是一个完整的 .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
于 2012-06-08T06:19:33.060 回答