【问题标题】:Get CLGeocoder values only in english仅以英文获取 CLGeocoder 值
【发布时间】: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;
         }
     }];
}

【问题讨论】:

    标签: ios objective-c geolocation


    【解决方案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】:

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

      【讨论】:

        猜你喜欢
        • 2021-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-02
        • 2012-05-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多