MKReverseGeocoder 返回的城市不是城市?

发布于 2024-12-03 05:49:02 字数 2758 浏览 0 评论 0原文

您好,我正在使用 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];

  }

}

Hi i am using mkreversegeocoder to find the users current city.It works but when i try it in another location it gives the names of borough/district in my country (Turkey).For example it gives "istanbul" correct but there is no city name like "susurluk" in my country it is a district.Any idea why it returns me districts instead of city name? if you want i would post my code's

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

  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2024-12-10 05:49:02

您可以通过使用 google 地图 api 来获得正确的答案,其中您必须发送该地点的纬度/经度,然后您将以 json/xml 的形式获得答案。请查看这个问题

使用 Google Map API 和 PHP 进行反向地理编码,以使用纬度、经度坐标获取最近的位置

You can get the correct answer by using the google map api in which you have to send the lat/long of the place and you will get answer in the form of json/xml. Please check out this question

Reverse Geocoding With Google Map API And PHP To Get Nearest Location Using Lat,Long coordinates

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文