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

Ios5 CLGeocoder只显示最近的苹果商店?,ios5,clgeocoder,Ios5,Clgeocoder,CLGeocoder现在就是这么粗糙吗?我期待的是一个相对接近街道地址的东西。我正在5.1模拟器上测试,并使用ARC。我现在做了一个快速测试项目,如果有帮助,可以使用以下内容: - (IBAction)getLocationPressed { if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kCLAut

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

谢谢

是的!事实证明,我只是没有在模拟器中启用定位。如果我在我的设备上测试它,它实际上是有效的。下面是一个使用完整的.m文件的示例,如果它对任何人都有帮助的话。我设置了两个标签和一个圆形的rect按钮,您将从代码中看到:

#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
#使用_位置(@“查找最近的小部件”)定义原因
@界面视图控制器()
@属性(弱、非原子)IBUILabel*位置标签;
@属性(强,非原子)CLLocationManager*locationManager;
@属性(强,非原子)CLGeocoder*geoCoder;
@结束
@实现视图控制器
#pragma标记-getter/setter
@合成locationLabel=\u locationLabel;
@综合locationManager=\u locationManager;
@合成地理编码器=_地理编码器;
//根据需要延迟实例化
-(CLGeocoder*)地理编码器{
如果(!\u geoCoder)\u geoCoder=[CLGeocoder new];
返回式地理编码器;
}
-(CLLocationManager*)locationManager{
如果(!\u locationManager)\u locationManager=[CLLocationManager新建];
返回位置管理器;
}
#pragma标记-目标/行动方法
-(iAction)ClearLocation按下{
self.locationLabel.text=@;
}
-(iAction)getLocationPressed{
如果([CLLocationManager LocationServiceEnabled]&&
[CLLocationManager授权状态]==kCLAuthorizationStatusAuthorized){
[self.geoCoder reversegeocolocation:self.locationManager.location completionHandler:
^(NSArray*放置标记,NSError*错误){
//注意:不能保证在主线程上调用CLGeocodeCompletionHandler
dispatch\u async(dispatch\u get\u main\u queue(),^{
NSLog(@“placemarks计数:%d”,[placemarks计数]);
CLPlacemark*placemark=[placemarks objectAtIndex:0];
//注意:如果指定了不良位置,则给定位置可能有多个位置标记
NSString*currentAddress=[[placemark.addressDictionary valueForKey:@“FormattedAddressLines”]组件由字符串连接:@“,”];
NSLog(@“我目前在%@”,currentAddress);
self.locationLabel.text=当前地址;
});
}];
}
}
#pragma标记-CLLocationManager委托方法
-(无效)locationManager:(CLLocationManager*)经理
DidUpdateLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)旧位置{
//做点什么。。。
}
-(无效)locationManager:(CLLocationManager*)经理
didFailWithError:(n错误*)错误{
if(error.code==kCLErrorDenied){
UIAlertView*警报=[[UIAlertView alloc]initWithTitle:@“错误!”
消息:@“如果未启用位置服务,此操作将无法工作”
代表:无取消按钮:@“确定”其他按钮:无];
[警报显示];
}
}
#pragma标记-生命周期方法
-(无效)viewDidLoad{
[超级视图下载];
self.locationManager.delegate=self;
self.locationManager.purpose=使用位置的原因;
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;//kCLLocationAccuracyNearestTenMeters;
[self.locationManager startUpdatingLocation];
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation{
返回YES;
}
-(无效)视图卸载{
[self-setLocationLabel:无];
[self-setLocationManager:无];
[自动设置地理编码器:无];
[超级视频下载];
}
@结束