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



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

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

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

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

最新更新