iOS6 CLPlacemark如何显示街道名称

4

我希望能显示用户位置的完整街道名称,但使用以下代码只能显示城市名称:

-(void) reverseGeocode:(CLLocation *)location{
   CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {    
    if(error) {
        NSLog(@"Error");
        return;
    }  
    if(placemarks) {
        CLPlacemark *placemark = placemarks [0];
        NSArray *lines = placemark.addressDictionary[ @"FormattedAddressLines"];
        NSString *addressString = [lines componentsJoinedByString:@"\n"];
        NSLog(@"Address: %@", addressString);
    }
}];}

有什么办法可以获取街道名称吗?
3个回答

10

我实际上使用这段代码来获取完整的位置信息:

@property (nonatomic,strong) CLPlacemark *placemark;
@property (strong) CLLocationManager *gps;
@property (strong) CLGeocoder *geocoder;

- (void)viewDidLoad
{
    [super viewDidLoad];

    gps = [[CLLocationManager alloc] init];
    geocoder = [[CLGeocoder alloc] init];
    gps.delegate = self;
    gps.desiredAccuracy = kCLLocationAccuracyBest;
    [gps startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];

                aLabel.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@",
                                placemark.thoroughfare,
                                placemark.subThoroughfare,
                                placemark.postalCode,
                                placemark.locality,
                                placemark.country];

        } else {
            NSLog(@"%@", error.debugDescription);
        }
    }];
}

但是如果需要,您可以使用其他CLPlacemark属性来获取更多信息:

来自CLPlacemark.h:

@property (nonatomic, readonly) NSString *name; // eg. Apple Inc.
@property (nonatomic, readonly) NSString *thoroughfare; // street address, eg. 1 Infinite Loop
@property (nonatomic, readonly) NSString *subThoroughfare; // eg. 1
@property (nonatomic, readonly) NSString *locality; // city, eg. Cupertino
@property (nonatomic, readonly) NSString *subLocality; // neighborhood, common name, eg. Mission District
@property (nonatomic, readonly) NSString *administrativeArea; // state, eg. CA
@property (nonatomic, readonly) NSString *subAdministrativeArea; // county, eg. Santa Clara
@property (nonatomic, readonly) NSString *postalCode; // zip code, eg. 95014
@property (nonatomic, readonly) NSString *ISOcountryCode; // eg. US
@property (nonatomic, readonly) NSString *country; // eg. United States
@property (nonatomic, readonly) NSString *inlandWater; // eg. Lake Tahoe
@property (nonatomic, readonly) NSString *ocean; // eg. Pacific Ocean
@property (nonatomic, readonly) NSArray *areasOfInterest; // eg. Golden Gate Park

1
谢谢您的回答,不幸的是,我得到了null,没有显示任何值。那么问题出在哪里? - Al_fareS
我更新了答案并提供了更多信息。你也这样做了吗?请记住,如果没有互联网连接,地址无法获取。 - DrKey
你能解释一下为什么你使用 placemark = [placemarks lastObject],而其他人实际上却得到了 placemarks 数组中的第一个对象吗?谢谢! - wz366

1

Try this :

CLPlacemark *placemark = placemarks [0];
NSArray *lines = placemark.addressDictionary[ @"FormattedAddressLines"];
        NSString *addressString = [placemark.addressDictionary objectForKey:(NSString*) kABPersonAddressStreetKey];
        NSLog(@"Address: %@", addressString);


NSString *street = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *city = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressCityKey];
NSString *state = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressStateKey];
NSString *country = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressCountryKey];
NSString *zip = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressZIPKey];

kABPersonAddressStreetKey 是未声明的标识符,声明在哪里? - Al_fareS
@Al_fareS import AddressBook.ABPerson @Al_fareS 导入AddressBook.ABPerson - ma11hew28

0

不要忘记添加 AddressBook 框架和:

#import <AddressBook/AddressBook.h>

还有供您使用的:

if(placemarks) {
  CLPlacemark *placemark = [placemarks firstObject];
  NSString *addressString = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressStreetKey];
  NSLog(@"Address: %@", addressString);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接