2012-06-08 55 views
0

我试图确定使用CLLocationManager和CLGeocoder了iPhone用户的位置。CLGeocoder不返回任何

我CLLocationManager似乎运作良好,但不幸的是,CLGeocoder不这样做,我想它做的事。

一些代码摘录:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 

    [self.locationManager stopUpdatingLocation]; 

    NSLog(@"Location updated to: %@",newLocation); 

    [self.geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { 

     NSLog(@"Reverse geocoding finished"); 

     self.currentPlacemark = [placemarks objectAtIndex:0]; 
     NSLog(@"%@",self.currentPlacemark.locality); 
     NSLog(@"%@",self.currentPlacemark.ISOcountryCode); 

    }]; 
} 

- (void)getCurrentLocation { 

    NSLog(@"Determining current location"); 

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

    [self.locationManager startUpdatingLocation]; 
} 

- (void)viewDidLoad { 

    [super viewDidLoad]; 
    [self getCurrentLocation]; 
} 

self.currentPlacemarkCLPlacemark对象。

self.geocoderCLGeocoder对象。

self.locationManagerCLLocationManager对象。

我错过了什么?为什么这不起作用? 老实说,我花了几个小时,我慢慢开始变得绝望......

+0

你能更具体地说“不做我想做的事”吗?特别是,它在做什么?还是不行? – gaige

+0

当然,抱歉在这一点不清楚:completionHandler块永远不会被输入(或换句话说:我从来没有看到日志语句“反向地理编码已完成”) – Patrick

+1

你有alloc和初始化你的地理编码器吗? –

回答

3

是的,你应该alloc和初始化你的地理编码器。

self.geocoder = [[[CLGeocoder alloc] init] autorelease]; 
[self.geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { 

    NSLog(@"Reverse geocoding finished"); 

    self.currentPlacemark = [placemarks objectAtIndex:0]; 
    NSLog(@"%@",self.currentPlacemark.locality); 
    NSLog(@"%@",self.currentPlacemark.ISOcountryCode); 

}];