Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLLocationManager, how to get full name of city or State

i am using CLLocationManager class in my iPad App to get current location.

i am using below code to get Address details,

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSUserDefaults *oIsLocation = [NSUserDefaults standardUserDefaults];
    if([oIsLocation boolForKey:@"IsLocation"])
    {
        NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
        if (locationAge > 5.0) return;

        if (newLocation.horizontalAccuracy < 0) return;

        if (bestEffortAtLocation == nil || bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy)
        {
            if(bestEffortAtLocation.coordinate.latitude != newLocation.coordinate.latitude && bestEffortAtLocation.coordinate.longitude != newLocation.coordinate.longitude)
            {
                [self saveLocation:newLocation];
                self.bestEffortAtLocation = newLocation;
                [self saveUserLocation];
            }
        }
    }
}

i am doing reverse geocoding to get Details as below:

                       NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode);
                       NSLog(@"placemark.country %@",placemark.country);
                       NSLog(@"placemark.postalCode %@",placemark.postalCode);
                       NSLog(@"placemark.administrativeArea %@",placemark.administrativeArea);
                       NSLog(@"placemark.locality %@",placemark.locality);
                       NSLog(@"placemark.subLocality %@",placemark.subLocality);
                       NSLog(@"placemark.subThoroughfare %@",placemark.subThoroughfare);

placemark.locality gives city name but not Full name like North Carolina only NC.

when my clients used app in USA, instead of full name like 'North Carolina' they get NC,Saint Charles get St. Charles ETC.

is there any way to get full names?

like image 979
user2813740 Avatar asked Jul 29 '15 12:07

user2813740


1 Answers

Following code which returns all that details into CLPlacemark.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{

    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:newLocation
                   completionHandler:^(NSArray *placemarks, NSError *error) {
                       for (CLPlacemark *placemark in placemarks) {

                           NSLog(@"%@",[placemark locality]);

                           CLPlacemark *placemark = [placemarks objectAtIndex:0];

                           NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode);
                           NSLog(@"placemark.country %@",placemark.country);
                           NSLog(@"placemark.postalCode %@",placemark.postalCode);
                           NSLog(@"placemark.administrativeArea %@",placemark.administrativeArea);
                           NSLog(@"placemark.locality %@",placemark.locality);
                           NSLog(@"placemark.subLocality %@",placemark.subLocality);
                           NSLog(@"placemark.subThoroughfare %@",placemark.subThoroughfare);

                       }
                   }];
}

May this helps lot.

like image 177
Nimit Parekh Avatar answered Sep 30 '22 07:09

Nimit Parekh