CLGeocoder不会返回任何内容

时间:2012-06-08 11:13:45

标签: objective-c

我正在尝试使用CLLocationManager和CLGeocoder来确定iPhone用户的位置。

我的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.currentPlacemark CLPlacemark 对象。

self.geocoder CLGeocoder 对象。

self.locationManager CLLocationManager 对象。

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

1 个答案:

答案 0 :(得分:3)

是的,您应该分配并初始化地理编码器。

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); 

}];