Search code examples
objective-cgpsclgeocoder

Get State from GPS Coordinates


I have used CLLocationManager to get the current GPS coordinates of a user.

How would I use CLGeoCoder to convert these coordinates into just the current State the user is in?

Thanks.


Solution

  • Use the -reverseGeocodeLocation:completionHandler method, then inspect the administrativeArea property of the returned placemark.

    In code:

    /* Get a CLLocation into `location` */
    CLGeocoder *gc = [[CLGeocoder alloc] init];
    [gc reverseGeocodeLocation:location completionHandler:^(NSArray *placemark, NSError *error)
    {
      if(!placemark)
      {
        /* handle error */
      }
      else
      {
        CLPlacemark *pm = placemark[0];
        NSLog(@"%@", pm.administrativeArea);
      }
    }