0

我正在尝试使用 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对象。

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

4

1 回答 1

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

}];
于 2012-06-08T11:24:15.783 回答