Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

iOS6 CLPlacemark to show street name

I want to show the full street name of the user location, with the below code I can only show the city name :

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

Any ideas on how I can get the street name ?

12 Answers

According to the documentation: https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLPlacemark_class/Reference/Reference.html

Under "Accessing the Placemark Attributes" There is a property called "thoroughfare".

Check it out :)

Thank you for your answer ! Now, I used this code, but I am getting (null) any ideas about what's wrong ?

CLGeocoder *geocoder = [[CLGeocoder alloc]init];
   [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
   if(error) {
        NSLog(@"Error");
        return;
    }
   if(placemarks) {
    CLPlacemark *placemark = [placemarks lastObject];
    NSLog(@"%@", placemark.thoroughfare); }
    }];

Try:

if(placemarks) {
    CLPlacemark *placemark = [placemarks objectAtIndex:0];
    NSLog(@"%@", placemark.thoroughfare); }

or:

if(!error) {
         CLPlacemark *placemark = [placemarks objectAtIndex:0];
         NSLog(@"%@", placemark.thoroughfare); }
    }else {
       //error
    }];

The same issue :s (null)

I edited my answer

Same issue, I don't know how to get rid form it :s Knowing that if I do :

    NSLog(@"%@", placemark);

I get the full address with the coordinates....

Aaah. I think I know what the problem is (if my last bit of code doesn't work) - If you log out you 'location'-property, that will be nil. And the address of nil, is nil.

instead of adding location as a parameter, try this:

CLGeocoder *geocoder = [[CLGeocoder alloc]init];
   [geocoder reverseGeocodeLocation:self.mapView.userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
   if(error) {
        NSLog(@"Error");
        return;
    }
   if(placemarks) {
    CLPlacemark *placemark = [placemarks lastObject];
    NSLog(@"%@", placemark.thoroughfare); }
    }];

You'd have to add this in you H file:

@property (strong, nonatomic) MKMapView *mapView;

and import the mapkit framework if you haven't already: #import <MapKit/MapKit.h>

EDIT If you're just using the coreLocation framework -never mind this code, you shouldn't have to use mapview to get it to work

Now, it is giving to me : Error

Probably because mapview and it's delegates is not implemented. But never mind that code. Just using CLLocation should work perfectly - I'll make a tiny example later, but now it's breakfast :)

Interesting! I made a test project. At first I received the same error as you (null). THEN, I switched from IOS simulator to a real iPhone - and suddenly it worked! I don't know why this is - but the problem obviously has something to do with the simulator (at least in my case)

Well, I run in my iPhone too, but I am getting only null

(i postet the wrong link) - Ill find the right one Try it out on your iPhone

Here you go: https://www.dropbox.com/sh/rdaaclptwi02zxn/4O4_OcryGe

Thank you for the efforts, but unfortunately I am still getting (null) :s By the way I am running on iOS6, will this be a problem ?

I don't see why it would. There is nothing in Apple's documentation to back that up. Considering it only worked on my iphone which is running IOS7, it's a little bit hard to test with IOS6