Cllocationmanager 如何从CLPlacemark获取zipCode,我得到它总是空的?
下面是一段示例代码:Cllocationmanager 如何从CLPlacemark获取zipCode,我得到它总是空的?,cllocationmanager,Cllocationmanager,下面是一段示例代码: [_geoCoder reverseGeocodeLocation:self.locationManager.location // You can pass aLocation here instead completionHandler:^(NSArray *placemarks, NSError *error) { dispatch_async(dispatch_get_main_queue(),^
[_geoCoder reverseGeocodeLocation:self.locationManager.location // You can pass aLocation here instead
completionHandler:^(NSArray *placemarks, NSError *error) {
dispatch_async(dispatch_get_main_queue(),^ {
// do stuff with placemarks on the main thread
if (placemarks.count == 1) {
CLPlacemark *place = [placemarks objectAtIndex:0];
// NSLog([place postalCode]);
NSString* addressString1 = [place thoroughfare];
addressString1 = [addressString1 stringByAppendingString:[NSString stringWithFormat:@"%@",[place.addressDictionary objectForKey:(NSString*)kABPersonAddressZIPKey]]];//
NSLog(@"%@",addressString1);// is null ??
NSlog(@"%@",place.postalCode);//is null ??
//[self performSelectorInBackground:@selector(log) withObject:nil];
}
});
}];
我猜你可能只有一个“坏”坐标,没有邮政编码。请尝试以下代码段。应在末尾显示各种信息和邮政编码“80802”:
CLLocationCoordinate2D myCoordinates = { 48.167222, 11.586111 };
CLLocation *location = [[CLLocation alloc]initWithLatitude:myCoordinates.latitude longitude:myCoordinates.longitude];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"%d Placemarks %@", placemarks.count, placemarks);
CLPlacemark *place = [placemarks objectAtIndex:0];
NSLog(@"Placemark %@", place);
NSLog(@"Address Dictionary: %@", place.addressDictionary);
NSLog(@"Zip key %@", [place.addressDictionary objectForKey:(NSString *)kABPersonAddressZIPKey]);
}];
在第一行中插入不同的坐标,然后自己进行实验。@Klass,这段代码在很多lat-long中都不起作用。它只对你有效。还有别的办法吗?@Roshni,这在很大程度上取决于苹果关于不同国家的数据。在美国和德国,数据似乎相当好。另一种选择是使用谷歌地图API及其反向地理编码器: