简体   繁体   English

如何通过iOS SDK上的给定邮政编码(ZIP)获取国家/地区名称

[英]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. 核心位置框架(CoreLocation.framework)为应用程序提供位置和标题信息。 For location information, the framework uses the onboard GPS, cell, or Wi-Fi radios to find the user's current longitude and latitude. 对于位置信息,框架使用板载GPS,小区或Wi-Fi无线电来查找用户当前的经度和纬度。

I hope there is a class on iOS SDK, I really don't want to use one of the Google Maps APIs 我希望在iOS SDK上有一个课程,我真的不想使用其中一个Google Maps API

Yes, your solution can be found within the iOS SDK. 是的,您的解决方案可以在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") 我不知道iOS是否支持所有国家的邮政编码,但它肯定适用于英国(例如邮政编码“YO258UH”)和加拿大(“V3H5H1”)

Michael Dautermann's answer is correct, just adding a code for swift (v4.2) if anyone comes to this post looking for it: 迈克尔·道特曼的回答是正确的,只要为swift(v4.2)添加代码,如果有人来这篇文章寻找它:

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

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

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