iOS geocodeAddressString为postalCode返回null值

时间:2014-05-21 14:43:22

标签: ios geocoding

我正在使用城市名称(例如西雅图,洛杉矶等)调用geocodeAddressString。在Xcode模拟器中,这将返回一个正确的地标值,但没有postalCode:

[geocoder geocodeAddressString:placename completionHandler:^(NSArray *placemarks, NSError *error) {
    //Error checking
    NSLog(@"placemarks %i",[placemarks count]);

    if ([placemarks count] == 1) { // one city returned. set location accordingly
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        NSLog(@"one location found %@",placemark);
        [self setUserLocation:placemark];
    } 

}];

输出地标值:

- (void)setUserLocation:(CLPlacemark *)placemark {
  NSLog(@"found zip %@",placemark.postalCode);
  NSLog(@"found city %@",placemark.locality);
  NSLog(@"found state %@",placemark.administrativeArea);
  NSLog(@"found country %@",placemark.ISOcountryCode);
  NSLog(@"found lat %f",placemark.region.center.latitude);
  NSLog(@"found long %f",placemark.region.center.longitude);
}

显示以下内容:

2014-05-21 07:28:12.378 jobagent[5157:60b] entered location seattle
2014-05-21 07:28:12.378 jobagent[5157:60b] placename seattle
2014-05-21 07:28:12.992 jobagent[5157:60b] placemarks 1
2014-05-21 07:28:12.993 jobagent[5157:60b] one location found Seattle, WA, United States @   <+47.60620950,-122.33207080> +/- 100.00m, region CLCircularRegion (identifier:'<+47.56346943,-122.32853603> radius 26395.84', center:<+47.56346943,-122.32853603>, radius:26395.84m)
2014-05-21 07:28:12.993 jobagent[5157:60b] found zip (null)
2014-05-21 07:28:12.994 jobagent[5157:60b] found city Seattle
2014-05-21 07:28:12.994 jobagent[5157:60b] found state WA
2014-05-21 07:28:12.995 jobagent[5157:60b] found country US
2014-05-21 07:28:12.995 jobagent[5157:60b] found lat 47.563469
2014-05-21 07:28:12.996 jobagent[5157:60b] found long -122.328536

1 个答案:

答案 0 :(得分:0)

CLGeocoder并不仅仅针对某个城市返回postalCode,因为有多个postalCode

要获得postalCode,您需要更具体,例如撰写街道