如何从CLPlacemark获取zipCode,我得到它总是为空?

时间:2013-07-13 16:02:02

标签: cllocationmanager

这是一段示例代码:

[_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];
                          // NSLog([place postalCode]);
                          NSString* addressString1 = [place thoroughfare];
                           addressString1 = [addressString1 stringByAppendingString:[NSString stringWithFormat:@"%@",[place.addressDictionary objectForKey:(NSString*)kABPersonAddressZIPKey]]];//
                           NSLog(@"%@",addressString1);// is null ??
                           NSlog(@"%@",place.postalCode);//is null ??

                           //[self performSelectorInBackground:@selector(log) withObject:nil];
                       }

                   });
               }];

1 个答案:

答案 0 :(得分:0)

我猜你可能只有一个'坏'坐标,没有邮政编码。请尝试以下代码段。它应该显示各种信息,最后是邮政编码'80802':

CLLocationCoordinate2D myCoordinates = { 48.167222, 11.586111 };
CLLocation *location = [[CLLocation alloc]initWithLatitude:myCoordinates.latitude longitude:myCoordinates.longitude];

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
    NSLog(@"%d Placemarks %@", placemarks.count, placemarks);
    CLPlacemark *place = [placemarks objectAtIndex:0];
    NSLog(@"Placemark %@", place);
    NSLog(@"Address Dictionary: %@", place.addressDictionary);
    NSLog(@"Zip key %@", [place.addressDictionary objectForKey:(NSString *)kABPersonAddressZIPKey]);

}];

在第一行插入不同的坐标并亲自体验。