how to localize address result from reverseGeocodeLocation?

前端 未结 4 1368
孤独总比滥情好
孤独总比滥情好 2021-01-31 09:55

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 l

相关标签:
4条回答
  • 2021-01-31 10:16

    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"

    0 讨论(0)
  • 2021-01-31 10:22

    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));
    
    0 讨论(0)
  • 2021-01-31 10:24

    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")
        }
    })
    
    0 讨论(0)
  • 2021-01-31 10:24

    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.

    0 讨论(0)
提交回复
热议问题