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

我是研究僧i 提交于 2019-12-10 22:09:03

问题


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:


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")




回答2:


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
                }
            }
        }
    }
}


来源:https://stackoverflow.com/questions/33459973/how-to-get-country-name-by-given-postal-code-zip-on-ios-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!