how to localize address result from reverseGeocodeLocation?
Asked Answered
V

4

26

My iphone app supposed to resolve address based on latitude and longitude of the user. reverseGeocodeLocation works fine, but results are in english.

Is there a way to localize the results to other languages?

couldnt find any information about it at apple or anywhere else.

The code I use is:

CLGeocoder *geocoder = [[[CLGeocoder alloc] init] autorelease];
CLLocation *location = [[[CLLocation alloc] 
       initWithLatitude:coord.latitude longitude:coord.longitude] autorelease];

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
    NSLog(@"reverseGeocodeLocation:completionHandler: Completion Handler called!");

    if (error){
        NSLog(@"Geocode failed with error: %@", error);
        [self displayError:error];
        return;
    }
    if(placemarks && placemarks.count > 0)
    {
      //do something
        CLPlacemark *topResult = [placemarks objectAtIndex:0];

        NSString *addressTxt = [NSString stringWithFormat:@"%@ %@,%@ %@", 
           [topResult subThoroughfare],[topResult thoroughfare],
           [topResult locality], [topResult administrativeArea]];
    }
}
Vanderbilt answered 10/1, 2012 at 19:45 Comment(4)
CLGeocoder is based on the google geocoder, which can produce localized results if the language option is set in the URL paramters. I use the google api, and I have results come back that appear to be localized in other languages.This may be obvious, but are you setting the language of the device to something other than English? What else have you tried?Plasticize
yes! it worked changing the language, but the question still remain if its possible to force the results to specific language ?Vanderbilt
From the documentation, I don't think so. If you haven't received fuller answer in a few hours, I'll show you some code that might help you. I don't have it right in front of me, though.Plasticize
@user513790, did you ever get solution about how to force the results to a specific language? For us, it should be always in English? Based on my current research, it seems impossible.Tuinenga
C
13

i found how to localize country name, maybe it'll help:

CLPlacemark *placemark;

NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: placemark.ISOcountryCode forKey: NSLocaleCountryCode]];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSString *country = [usLocale displayNameForKey: NSLocaleIdentifier value: identifier];

insert any country id instead @"en_US"

Cocklebur answered 26/12, 2013 at 17:4 Comment(1)
Do you know how can i do the same thing for placemark.locality? ex: "Roma" -> "Rome"Citarella
P
6

Solution for Swift 4, iOS 11

You can force to get geocoding results in chosen locale language by setting argument: preferredLocale: Locale.init(identifier: "en_US").

CLGeocoder().reverseGeocodeLocation(location, preferredLocale: Locale.init(identifier: "en_US"), completionHandler: {(placemarks, error) -> Void in
    print(location)

    if error != nil {
        print("Reverse geocoder failed with error" + error!.localizedDescription)
        return
    }

    if placemarks!.count > 0 {
        let pm = placemarks![0]
        print(pm.administrativeArea!, pm.locality!)
    }
    else {
        print("Problem with the data received from geocoder")
    }
})
Phyllode answered 25/1, 2019 at 23:36 Comment(0)
P
1

Mattt Thompson has a great writeup on using a method from the AddressBookUI.framework for localizing addresses on his site NSHipster. He also has a formatting library on github which contains a class for doing this type of localization which uses the AddressBookUI.framework approach he has described.

Plenish answered 1/12, 2012 at 20:39 Comment(0)
A
0

Since iOS11, apple provides us with another API which can set locale to print localized language.

- (void)reverseGeocodeLocation:(CLLocation *)location preferredLocale:(nullable NSLocale *)locale
 completionHandler:(CLGeocodeCompletionHandler)completionHandler API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));
Agio answered 12/9, 2018 at 2:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.