0

有人知道如何从这段代码中分离出城市、州和地址吗?它返回整个地址,但我只想要城市和州。

//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];

 }];
4

2 回答 2

0

CLPlacemark 具有 和 等locality属性administrativeArea。请参阅文档以了解它们是什么,但您需要进行实验以了解它们如何将地址解析为其组件。此外,它addressDictionary是地址簿格式,所以里面有城市和州的键;你的错误是把它变成一个字符串而不是检查字典的结构。

于 2012-12-01T00:53:09.007 回答
-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);
    }
} ];
于 2012-12-01T05:40:13.823 回答