简体   繁体   中英

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

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

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 (eg postal code of "YO258UH") and Canada ("V3H5H1")

Michael Dautermann's answer is correct, just adding a code for swift (v4.2) if anyone comes to this post looking for it:

@IBAction func getLocationTapped(_ sender: Any) {

    guard let zipcode = zipcodeTxtField.text else {
        print("must enter zipcode")
        return
    }

    CLGeocoder().geocodeAddressString(zipcode) { (placemarks, error) in
        if let error = error{
            print("Unable to get the location: (\(error))")
        }
        else{
            if let placemarks = placemarks{

                // get coordinates and city
                guard let location = placemarks.first?.location, let city = placemarks.first?.locality else {
                    print("Location not found")
                    return
                }


                print("coordinates: -> \(location.coordinate.latitude) , \(location.coordinate.longitude)")
                print("city: -> \(city)")

                if let country = placemarks.first?.country{
                     print("country: -> \(country)")
                }

                //update UI on main thread
                DispatchQueue.main.async {
                    self.countryLbl.text = country
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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