4

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 ?

3 Answers 3

10

I'm actually using this code to get a complete location:

@property (nonatomic,strong) CLPlacemark *placemark;
@property (strong) CLLocationManager *gps;
@property (strong) CLGeocoder *geocoder;

- (void)viewDidLoad
{
    [super viewDidLoad];

    gps = [[CLLocationManager alloc] init];
    geocoder = [[CLGeocoder alloc] init];
    gps.delegate = self;
    gps.desiredAccuracy = kCLLocationAccuracyBest;
    [gps startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];

                aLabel.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@",
                                placemark.thoroughfare,
                                placemark.subThoroughfare,
                                placemark.postalCode,
                                placemark.locality,
                                placemark.country];

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

but you can get more if needed by using other CLPlacemark properties:

From CLPlacemark.h:

@property (nonatomic, readonly) NSString *name; // eg. Apple Inc.
@property (nonatomic, readonly) NSString *thoroughfare; // street address, eg. 1 Infinite Loop
@property (nonatomic, readonly) NSString *subThoroughfare; // eg. 1
@property (nonatomic, readonly) NSString *locality; // city, eg. Cupertino
@property (nonatomic, readonly) NSString *subLocality; // neighborhood, common name, eg. Mission District
@property (nonatomic, readonly) NSString *administrativeArea; // state, eg. CA
@property (nonatomic, readonly) NSString *subAdministrativeArea; // county, eg. Santa Clara
@property (nonatomic, readonly) NSString *postalCode; // zip code, eg. 95014
@property (nonatomic, readonly) NSString *ISOcountryCode; // eg. US
@property (nonatomic, readonly) NSString *country; // eg. United States
@property (nonatomic, readonly) NSString *inlandWater; // eg. Lake Tahoe
@property (nonatomic, readonly) NSString *ocean; // eg. Pacific Ocean
@property (nonatomic, readonly) NSArray *areasOfInterest; // eg. Golden Gate Park
3
  • 1
    Thanks for the answer, unfortunately, I am getting null, no values are shown. What's wrong then ?
    – Al_fareS
    Feb 1, 2014 at 11:23
  • I updated the answer with more info. Did you do the same? Remember that without an internet connection the address can't be obtained.
    – DrKey
    Feb 1, 2014 at 11:33
  • Could you explain why you used placemark = [placemarks lastObject] while some other people are actually getting the first object in the array of placemarks? Thanks!
    – wz366
    Apr 10, 2014 at 6:11
1

Try this :

CLPlacemark *placemark = placemarks [0];
NSArray *lines = placemark.addressDictionary[ @"FormattedAddressLines"];
        NSString *addressString = [placemark.addressDictionary objectForKey:(NSString*) kABPersonAddressStreetKey];
        NSLog(@"Address: %@", addressString);


NSString *street = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *city = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressCityKey];
NSString *state = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressStateKey];
NSString *country = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressCountryKey];
NSString *zip = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressZIPKey];
2
  • kABPersonAddressStreetKey is undeclared identifier, where is the declaration ?
    – Al_fareS
    Feb 1, 2014 at 10:45
  • @Al_fareS import AddressBook.ABPerson
    – ma11hew28
    Aug 30, 2014 at 4:32
0

don't miss to add AddressBook. Framework and add :

#import <AddressBook/AddressBook.h>

also for your use:

if(placemarks) {
  CLPlacemark *placemark = [placemarks firstObject];
  NSString *addressString = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressStreetKey];
  NSLog(@"Address: %@", addressString);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.