如何拆分反向地理编码组件?

时间:2012-12-01 00:48:09

标签: geocoding cllocation reverse-geocoding

任何人都知道如何从这段代码中拆分城市,州和地址?它返回整个地址,但我只想要城市和州。

//Geocoding Block
[_geoCoder2 reverseGeocodeLocation: _currentLocation2.location completionHandler:
 ^(NSArray *placemarks, NSError *error) {

     //Get nearby address
     CLPlacemark *placemark = [placemarks objectAtIndex:0];

     //String to hold address
     NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];

     //Print the location to console
     NSLog(@"I am currently at %@",locatedAt);

     //Set the label text to current location
     [_cityLabel setText:locatedAt];

 }];

2 个答案:

答案 0 :(得分:0)

CLPlacemark具有localityadministrativeArea等属性。查看文档以了解它们是什么,但您需要尝试查看它们如何将地址解析为其组件。此外,addressDictionary是地址簿格式,因此其中有城市和州的键;你的错误是把它变成一个字符串,而不是检查字典的结构。

答案 1 :(得分:-1)

// Reverse Geocoding
NSLog(@"Resolving the Address");
[_geoCoder2 reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
    NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
    if (error == nil && [placemarks count] > 0) {
        placemark = [placemarks lastObject];
        _cityLabel.text = [NSString stringWithFormat:@"%@\n%@\n",

                           placemark.locality,
                           placemark.administrativeArea];

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