【问题标题】:CLLocationManager, how to get full name of city or StateCLLocationManager,如何获取城市或州的全名
【发布时间】:2015-10-20 10:05:30
【问题描述】:

我在我的 iPad 应用程序中使用 CLLocationManager 类来获取当前位置。

我正在使用下面的代码来获取地址详细信息,

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSUserDefaults *oIsLocation = [NSUserDefaults standardUserDefaults];
    if([oIsLocation boolForKey:@"IsLocation"])
    {
        NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
        if (locationAge > 5.0) return;

        if (newLocation.horizontalAccuracy < 0) return;

        if (bestEffortAtLocation == nil || bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy)
        {
            if(bestEffortAtLocation.coordinate.latitude != newLocation.coordinate.latitude && bestEffortAtLocation.coordinate.longitude != newLocation.coordinate.longitude)
            {
                [self saveLocation:newLocation];
                self.bestEffortAtLocation = newLocation;
                [self saveUserLocation];
            }
        }
    }
}

我正在做反向地理编码以获取如下详细信息:

                       NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode);
                       NSLog(@"placemark.country %@",placemark.country);
                       NSLog(@"placemark.postalCode %@",placemark.postalCode);
                       NSLog(@"placemark.administrativeArea %@",placemark.administrativeArea);
                       NSLog(@"placemark.locality %@",placemark.locality);
                       NSLog(@"placemark.subLocality %@",placemark.subLocality);
                       NSLog(@"placemark.subThoroughfare %@",placemark.subThoroughfare);

placemark.locality 提供城市名称,但不提供全名,例如仅北卡罗来纳州 NC。

当我的客户在美国使用应用程序时,他们得到的是 NC,而不是像“北卡罗来纳州”这样的全名,而圣查尔斯得到的是 St. Charles ETC。

有没有办法得到全名?

【问题讨论】:

    标签: ios objective-c cllocationmanager cllocation


    【解决方案1】:

    以下代码将所有详细信息返回到CLPlacemark。

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
    
        CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
        [geoCoder reverseGeocodeLocation:newLocation
                       completionHandler:^(NSArray *placemarks, NSError *error) {
                           for (CLPlacemark *placemark in placemarks) {
    
                               NSLog(@"%@",[placemark locality]);
    
                               CLPlacemark *placemark = [placemarks objectAtIndex:0];
    
                               NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode);
                               NSLog(@"placemark.country %@",placemark.country);
                               NSLog(@"placemark.postalCode %@",placemark.postalCode);
                               NSLog(@"placemark.administrativeArea %@",placemark.administrativeArea);
                               NSLog(@"placemark.locality %@",placemark.locality);
                               NSLog(@"placemark.subLocality %@",placemark.subLocality);
                               NSLog(@"placemark.subThoroughfare %@",placemark.subThoroughfare);
    
                           }
                       }];
    }
    

    希望这对您有很大帮助。

    【讨论】:

    • 是的,我正在 [self saveLocation:newLocation];方法。我提到我得到了所有必需的细节......但我得到的不是像圣查尔斯这样的城市的全名,而是圣查尔斯。我需要全名。 place mark.locality 给出城市名称。来自印度,我无法弄清楚,但是当客户报告一些问题时,我遇到的问题是我没有得到城市的全名....有什么办法
    • @user2813740 你有解决办法吗
    猜你喜欢
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    相关资源
    最近更新 更多