Asi Givati
Asi Givati

Reputation: 1475

Get CLGeocoder values only in english

How can I get all the values (country, CITY(important) and thoroughfare) and from CLGeocoder only in english and ignore the language of the device?

My code:

-(void)updateLocations
{
    // Create a location manager
    locationManager = [[CLLocationManager alloc] init];
    // Set a delegate to receive location callbacks
    locationManager.delegate = self;
    // Start the location manager
    [locationManager startUpdatingLocation];

    //    [self.locationManager requestWhenInUseAuthorization];
    [locationManager requestAlwaysAuthorization];
}


-(void)updateLocationManager : (CLLocationManager *)manager
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:locationManager.location completionHandler:^(NSArray *placemarks, NSError *error)
     {
         if(placemarks && placemarks.count > 0)
         {
             CLPlacemark *placemark = [placemarks objectAtIndex:0];

             NSString *country = placemark.country;

             // Getting the City
             NSString *city = placemark.addressDictionary[@"City"];

             NSString *thoroughfare = [placemark thoroughfare];

         }

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

Upvotes: 1

Views: 621

Answers (3)

Zverusha
Zverusha

Reputation: 317

Try to change language and region on your device. It's works correct for me. I hope this will help

Upvotes: 0

Diego Jiménez
Diego Jiménez

Reputation: 1526

It is working for me, but you have to add the code like this:

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

CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {
    NSLog(@"lon - %@", [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
    NSLog(@"lat - %@",  [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
}

// Reverse Geocoding
NSLog(@"Resolving the Address");

NSArray *currentLanguageArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:@"en_US", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];

[_geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
    NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
    if (error == nil && [placemarks count] > 0) {
        _placemark = [placemarks lastObject];
        //addressLabel.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
        //                     placemark.subThoroughfare, placemark.thoroughfare,
        //                     placemark.postalCode, placemark.locality,
        //                     placemark.administrativeArea,
        //                     placemark.country];
        NSLog(@"%@ - %@", _placemark.locality, _placemark.country);

    } else {
        NSLog(@"%@", error.debugDescription);
    }

    [[NSUserDefaults standardUserDefaults] setObject:currentLanguageArray forKey:@"AppleLanguages"];
    [[NSUserDefaults standardUserDefaults] synchronize];

} ];}

In the sample code I am using this variables:

@property(nonatomic, retain) IBOutlet MKMapView *mapView;
@property(nonatomic, retain) CLLocationManager *locationManager;
@property(nonatomic, retain) CLGeocoder *geocoder;
@property(nonatomic, retain) CLPlacemark *placemark;

This sample code is giving the city name and the Country in English, I have not tested the city name if it is shown in english properly, but it is probably fine.

Hope it helps!!

Upvotes: 0

gabuh
gabuh

Reputation: 1166

Try changing the NSUserDefaults value for AppleLanguages key before querying for the location

NSArray *currentLanguageArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];

 //get the location

[[NSUserDefaults standardUserDefaults] setObject:currentLanguageArray forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];

Upvotes: 1

Related Questions