How to get Country name by given Postal Code (ZIP)

2019-07-05 11:43发布

Is it possible to get the country name by giving the postal code of the user?

I looked at the Core Location Framework, but it doesn't look to work the other way around by given the Postal Code and finding the Country name.

The Core Location framework (CoreLocation.framework) provides location and heading information to apps. For location information, the framework uses the onboard GPS, cell, or Wi-Fi radios to find the user’s current longitude and latitude.

I hope there is a class on iOS SDK, I really don't want to use one of the Google Maps APIs

1条回答
戒情不戒烟
2楼-- · 2019-07-05 12:29

Yes, your solution can be found within the iOS SDK.

Hook up a text field to this action:

- (IBAction)doSomethingButtonClicked:(id) sender
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:yourZipCodeGoesHereTextField.text completionHandler:^(NSArray *placemarks, NSError *error) {

        if(error != nil)
        {
            NSLog(@"error from geocoder is %@", [error localizedDescription]);
        } else {
            for(CLPlacemark *placemark in placemarks){
                NSString *city1 = [placemark locality];
                NSLog(@"city is %@",city1);
                NSLog(@"country is %@",[placemark country]);
                // you'll see a whole lotta stuff is available
                // in the placemark object here...
                NSLog(@"%@",[placemark description]);
            }
        }
    }];
}

I don't know if iOS supports postal codes for all countries, but it definitely works for the UK (e.g. postal code of "YO258UH") and Canada ("V3H5H1")

查看更多
登录 后发表回答