Easiest way of getting reverse geocoded current location from iOS
Asked Answered
C

4

11

I saw from another question here : Determine iPhone user's country that it is possible to get the current country the user of the iPhone is in.

And that is quite convenient for many uses. However, would it be possible to go even deeper and infer from iOS (if it has the information) which state or city the user is in as well?

I suppose reverse geocoding services would be the next step if things weren't possible.. Are there even such things as a reverse geocoding service you can hire for your app though?

Crispy answered 20/2, 2011 at 17:47 Comment(0)
C
5

I would start with the CLReverseGeocoder class.

This stackoverflow question gets the current city and can probably be adapted for your use.

Crabbed answered 20/2, 2011 at 17:50 Comment(1)
This method deprecated in iOS 5.Calamus
P
26

MKReverseGeocoder is deprecated in iOS 5, now it's CLGeocoder

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
   [self.locationManager stopUpdatingLocation];

   CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
   [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
       for (CLPlacemark * placemark in placemarks) {
           .... = [placemark locality];
        }    
    }];
}
Pappus answered 26/3, 2012 at 15:54 Comment(0)
K
24
CLGeocoder *geocoder = [[CLGeocoder alloc] init];

CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:21.1700
                                                    longitude:72.8300];

[geocoder reverseGeocodeLocation:newLocation
               completionHandler:^(NSArray *placemarks, NSError *error) {

                   if (error) {
                       NSLog(@"Geocode failed with error: %@", error);
                       return;
                   }

                   if (placemarks && placemarks.count > 0)
                   {
                       CLPlacemark *placemark = placemarks[0];

                       NSDictionary *addressDictionary =
                       placemark.addressDictionary;

                       NSLog(@"%@ ", addressDictionary);
                       NSString *address = [addressDictionary
                                            objectForKey:(NSString *)kABPersonAddressStreetKey];
                       NSString *city = [addressDictionary
                                         objectForKey:(NSString *)kABPersonAddressCityKey];
                       NSString *state = [addressDictionary
                                          objectForKey:(NSString *)kABPersonAddressStateKey];
                       NSString *zip = [addressDictionary 
                                        objectForKey:(NSString *)kABPersonAddressZIPKey];


                       NSLog(@"%@ %@ %@ %@", address,city, state, zip);
                   }

               }];

Result

{

  City = Surat;
  Country = India;
  CountryCode = IN;
  FormattedAddressLines =     (
    Surat,
    Gujarat,
    India
);
Name = Surat;
State = Gujarat;
} 
2012-12-20 21:33:26.284 CurAddress[4110:11603] (null) Surat Gujarat (null)
Khufu answered 20/12, 2012 at 16:7 Comment(1)
#import <AddressBook/AddressBook.h> you have to add this framework as well.Khufu
C
5

I would start with the CLReverseGeocoder class.

This stackoverflow question gets the current city and can probably be adapted for your use.

Crabbed answered 20/2, 2011 at 17:50 Comment(1)
This method deprecated in iOS 5.Calamus
B
3

Following codes can be easy to retrieve full-details.

 [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        if(placemarks.count){

        placeNameLabel.text = [placemarks[0] name];
        streetNumberLabel.text = [placemarks[0] subThoroughfare];
        streetLabel.text = [placemarks[0] thoroughfare];
        neighborhoodLabel.text = [placemarks[0] subLocality];
        cityLabel.text = [placemarks[0] locality];
        countyLabel.text = [placemarks[0] subAdministrativeArea];
        stateLabel.text = [placemarks[0] administrativeArea];    //or province 
        zipCodeLabel.text = [placemarks[0] postalCode];
        countryLabel.text = [placemarks[0] country];
        countryCodeLabel.text = [placemarks[0] ISOcountryCode];
        inlandWaterLabel.text = [placemarks[0] inlandWater];
        oceanLabel.text = [placemarks[0] ocean];
        areasOfInterestLabel.text = [placemarks[0] areasOfInterest[0]];
        }
    }];
Brenner answered 3/9, 2013 at 0:48 Comment(1)
The raw value of ".subThoroughfare" will contain a dash, i.e. \U2013, you must handle this in order to display a user friendly address to the user.Osborn

© 2022 - 2024 — McMap. All rights reserved.