CLLocationManager,如何获取城市或州的全名

5
我正在我的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提供了城市名称,但不会像North Carolina一样提供完整名称,只会提供NC。

当我的客户在美国使用应用程序时,他们得到的是缩写如'NC',而不是像'North Carolina'。Saint Charles则变成了St. Charles等。

有没有办法获取完整名称?

1个回答

7

以下代码将返回所有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);

                       }
                   }];
}

希望这能有所帮助。


1
是的,我正在我的[self saveLocation:newLocation];方法中进行。我提到了我获取了所有必要的细节......但是我得到的不是像Saint Charles这样的城市全名,而是St Charles。我需要完整的名称。place mark.locality给出城市名称。来自印度,我无法理解,但当客户报告一些问题时,我遇到了问题,即我没有获得城市的全名......有没有什么办法? - user2813740
@user2813740,你解决这个问题了吗? - Nassif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接