0

它说它“在 iOS 5 中已弃用”。

- (void)viewDidLoad {
    [super viewDidLoad];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder reverseGeocodeLocation:self.locationManager.location // You can pass aLocation here instead
                   completionHandler:^(NSArray *placemarks, NSError *error) {

                 dispatch_async(dispatch_get_main_queue() , ^ {
                           // do stuff with placemarks on the main thread

                           CLPlacemark *place = [placemarks objectAtIndex:0];

                           NSString *zipString = [place.addressDictionary valueForKey:@"ZIP"];

                           [self performSelectorInBackground:@selector(showWeatherFor:) withObject:zipString];



                       }
            }
 }
4

2 回答 2

14

MKReverseGeocoder在 iOS4 之后的所有固件中已弃用。这只是意味着它现在已经过时并且不赞成使用过时的类。改为使用CLGeocoder,如下所示:

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

        [geocoder reverseGeocodeLocation:self.locationManager.location // You can pass aLocation here instead 
                       completionHandler:^(NSArray *placemarks, NSError *error) {

                           dispatch_async(dispatch_get_main_queue(),^ {
                               // do stuff with placemarks on the main thread

                           if (placemarks.count == 1) {

                           CLPlacemark *place = [placemarks objectAtIndex:0];


                           NSString *zipString = [place.addressDictionary valueForKey:@"ZIP"];

                           [self performSelectorInBackground:@selector(showWeatherFor:) withObject:zipString];

                           }

     });

}];

如果你想对一对硬编码的坐标进行反向地理编码——

用您的纬度和经度初始化 CLLocation 位置:

    CLLocation *aLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

我还想指出,您仍然可以使用MKReverseGeocoder. 不过,它可能会在未来的 iOS 更新中被删除。

于 2012-08-06T16:36:22.453 回答
2

我不知道这是否回答了您的默认问题,但文档说要改用CLGeocoder

于 2012-08-06T16:24:54.370 回答