Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way of getting reverse geocoded current location from iOS

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?

like image 996
kamziro Avatar asked Feb 20 '11 17:02

kamziro


4 Answers

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];
        }    
    }];
}
like image 116
fengd Avatar answered Oct 31 '22 17:10

fengd


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)
like image 37
Ankit Vyas Avatar answered Oct 31 '22 18:10

Ankit Vyas


I would start with the CLReverseGeocoder class.

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

like image 5
Walter Avatar answered Oct 31 '22 18:10

Walter


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]];
        }
    }];
like image 3
Yi Jiang Avatar answered Oct 31 '22 17:10

Yi Jiang