简体   繁体   English

使用CLGeocoder获取邮政编码

[英]Getting Postal Code using CLGeocoder

I'm trying to get the postal code when my map region has changed by using CLGeocoder: 当我使用CLGeocoder更改了地图区域时,我尝试获取邮政编码:

CLLocation *location = [[CLLocation alloc] initWithLatitude:13.184098 longitude:77.725978];

    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
CLLocation *location=[[CLLocation alloc] initWithLatitude:13.184098 longitude:77.725978];
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
 {
     if (!(error))
     {
         CLPlacemark *placemark = [placemarks objectAtIndex:0];
         NSLog(@"placemark %@",placemark);

         NSString *Zipcode = [[NSString alloc]initWithString:placemark.postalCode];
         NSLog(@"%@",Zipcode);
     }
     else
     {
         NSLog(@"Geocode failed with error %@", error); // Error handling must required
     }
 }];
}

i am getting response like this 我正在这样回应

{ Country = India; CountryCode = IN; FormattedAddressLines = ( Amarwara, "Madhya Pradesh", India ); Name = Amarwara; State = "Madhya Pradesh"; SubAdministrativeArea = Chhindwara; SubLocality = Amarwara; }

I'm able to get the postal code for the first time. 我可以第一次获得邮政编码。 When the region changes, I try to reverse geocode the postal code. 当区域发生变化时,我会尝试对邮政编码进行地理编码。 I even tried setting the location explicitly (line 1) and it's still returning null postal code. 我什至尝试显式设置位置(第1行),但仍返回空邮政编码。 Can any one help this strange bug? 任何人都可以帮助解决这个奇怪的错误吗?

Try this 尝试这个

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:21.1700
                                                    longitude:72.8300];

[geocoder reverseGeocodeLocation:newLocation
               completionHandler:^(NSArray *placemarks, NSError *error) {

                   if (error) {
                       NSLog(@"Geocode failed with error: %@", error);
                       return;
                   }

                   if (placemarks && placemarks.count > 0)
                   {
                       CLPlacemark *placemark = placemarks[0];

                       NSDictionary *addressDictionary =
                       placemark.addressDictionary;

                       NSLog(@"%@ ", addressDictionary);
                       NSString *address = [addressDictionary
                                            objectForKey:(NSString *)kABPersonAddressStreetKey];
                       NSString *city = [addressDictionary
                                         objectForKey:(NSString *)kABPersonAddressCityKey];
                       NSString *state = [addressDictionary
                                          objectForKey:(NSString *)kABPersonAddressStateKey];
                       NSString *zip = [addressDictionary 
                                        objectForKey:(NSString *)kABPersonAddressZIPKey];


                       NSLog(@"%@ %@ %@ %@", address,city, state, zip);
                   }

               }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM