嗨,我正在使用 mkreversegeocoder 来查找用户当前的城市。它可以工作,但是当我在另一个位置尝试它时,它会给出我的国家(土耳其)的自治市镇/地区的名称。例如,它给出的“伊斯坦布尔”是正确的,但没有城市在我的国家,像“susurluk”这样的名字是一个地区。知道为什么它会返回我的地区而不是城市名称吗?如果你愿意,我会发布我的代码
编辑1:
-(void)viewDidLoad {
[super viewDidLoad];
manager=[[CLLocationManager alloc]init];
manager.delegate=self;
manager.desiredAccuracy=kCLLocationAccuracyBest;
[manager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)aManager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
MKReverseGeocoder *geocoder=[[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
geocoder.delegate=self;
[geocoder start];
[manager startUpdatingLocation];
}
-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
NSDictionary * addressDict= [placemark addressDictionary];
//NSString *country = [addressDict objectForKey:@"Country"];
NSString *city = [addressDict objectForKey:@"City"];
//change the country name into en_US
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
NSString *country1 = [usLocale displayNameForKey: NSLocaleCountryCode value: countryCode];
[[NSUserDefaults standardUserDefaults] setObject:country1 forKey:@"country"];
[[NSUserDefaults standardUserDefaults] setObject:city forKey:@"city"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {
if (error.code == kCLErrorHeadingFailure) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Problem"
message:@"Can't find the location!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alertView show];
[alertView release];
[manager stopUpdatingLocation];
}
}
-(void)locationManager:(CLLocationManager *)manager1 didFailWithError:(NSError *)error {
if (error.code != kCLErrorDenied) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Problem"
message:@"Can't find the location!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alertView show];
[alertView release];
[manager stopUpdatingLocation];
}
}