Question

I am using the following code to receive placemarks from CLGeocoder, and I want to display only the city in another textview on the same viewcontroller. How would I parse out the city from this array? It isn't an NSDictionary or anything, so I am stuck as to what to do.

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
    NSLog(@"reverseGeocodeLocation:completionHandler: Completion Handler called!");
    if (error){
        NSLog(@"Geocode failed with error: %@", error);
        return;
    }
    NSLog(@"Received placemarks: %@", placemarks);
}];

My log for a given placemark is as follows:

<__NSArrayM 0x1a0b3810>( 937–961 Sunnyvale Saratoga Rd, 937–961 Sunnyvale Saratoga Rd, Sunnyvale, CA 94087, United States @ <+37.35984860,-122.03235910> +/- 100.00m )

or

<__NSArrayM 0x19576ac0>( 5600 Van Nuys Blvd, 5600 Van Nuys Blvd, Van Nuys, CA 91401-4602, United States @ <+34.17257000,-118.44794450> +/- 100.00m, region (identifier <+34.17257001,-118.44794464> radius 57.64) <+34.17257001,-118.44794464> radius 57.64m )

How could I get the Sunnyvale or Van Nuys part out to display as a string?

Was it helpful?

Solution

Use CLPlacemark's properties

CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:yourLocation completionHandler:^(NSArray *placemarks, NSError *error) {
  CLPlacemark *placemark = [placemarks objectAtIndex:0];
  NSString *strCity = [placemark locality];
  NSLog(@"%@",strCity);
}];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top