如何从reverseGeocodeLocation本地化地址结果?

use*_*790 25 iphone xcode ios clgeocoder

我的iphone应用程序应该根据用户的经度和经度来解析地址.reverseGeocodeLocation工作正常,但结果是英文的.

有没有办法将结果本地化为其他语言?

无法在苹果或其他任何地方找到有关它的任何信息.

我使用的代码是:

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]];
    }
}
Run Code Online (Sandbox Code Playgroud)

roz*_*kin 12

我找到了如何本地化国家名称,也许它会有所帮助:

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];
Run Code Online (Sandbox Code Playgroud)

插入任何国家/地区ID而不是@"en_US"

  • 你知道我怎么能为placemark.locality做同样的事情?例如:"罗马" - >"罗马" (8认同)

d32*_*223 5

Swift 4、iOS 11的解决方案

您可以通过设置参数强制获取所选语言环境的地理编码结果: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")
    }
})
Run Code Online (Sandbox Code Playgroud)


Der*_*way 1

Mattt Thompson有一篇关于使用 AddressBookUI.framework 中的方法来本地化他的网站NSHipster上的地址的精彩文章。他还在 github 上有一个格式化库,其中包含一个用于执行此类本地化的类,该类使用他描述的 AddressBookUI.framework 方法。