CLLocationManager, how to get full name of city or State
Asked Answered
T

1

5

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?

Translatable answered 29/7, 2015 at 12:19 Comment(0)
C
7

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.

Comparison answered 29/7, 2015 at 12:38 Comment(2)
yeah i am doing in my [self saveLocation:newLocation]; method. i mentioned i am getting all required details..... but instead of full name of City like Saint Charles i am getting as St Charles. i need full name. place mark.locality gives city name. from india i could not make out but when Client reported some issue i got problem is i am not getting Full name of City.... is there any wayTranslatable
@Translatable did you get a solution for thisDykstra

© 2022 - 2024 — McMap. All rights reserved.