仅以英语获取CLGeocoder值

时间:2015-01-19 15:19:32

标签: ios objective-c geolocation

如何才能获得所有价值(国家,城市(重要)和通衢),只能用英语获取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;
         }
     }];
}

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

这对我有用,但你必须添加如下代码:

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

此示例代码以英文显示城市名称和国家/地区,如果以英语正确显示,我还没有测试城市名称,但它可能没问题。

希望它有所帮助!!

答案 2 :(得分:0)

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