仅以英文获取 CLGeocoder 值

Posted

技术标签:

【中文标题】仅以英文获取 CLGeocoder 值【英文标题】:Get CLGeocoder values only in english 【发布时间】:2015-01-19 15:19:32 【问题描述】:

我怎样才能从 CLGeocoder 中获取所有值(国家、城市(重要)和通道),并且只使用英语并忽略设备的语言?

我的代码:

-(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;
         
     ];

【问题讨论】:

【参考方案1】:

在查询位置之前尝试更改AppleLanguages 键的NSUserDefaults

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];

【讨论】:

我添加了 [[NSUserDefaults standardUserDefaults] synchronize]; 检查是否可以使用。 我试过了,还是一样。不是英语=\【参考方案2】:

它对我有用,但你必须像这样添加代码:

- (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];

 ];

在示例代码中我使用了这个变量:

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

这个示例代码给出了英文的城市名称和国家,我没有测试城市名称是否正确显示为英文,但它可能没问题。

希望对你有帮助!!

【讨论】:

【参考方案3】:

尝试更改您设备上的语言地区。它对我来说是正确的。我希望这会有所帮助

【讨论】:

以上是关于仅以英文获取 CLGeocoder 值的主要内容,如果未能解决你的问题,请参考以下文章

按最近位置排序 CLGeoCoder geocodeAddressString 的结果

INUIAddVoiceShortcutViewController 始终以英文显示

JAVA循环获取值

以不同语言显示引导日期选择器日期,然后在 asp.net razor 视图中以英文提交日期

获取 CLGeocoder 返回的确切州/省字符串

从 CLGeocoder 获取当前城市和国家?