如何在 iOS SDK 上通过给定的邮政编码 (ZIP) 获取国家名称
How to get Country name by given Postal Code (ZIP) on iOS SDK
是否可以通过提供用户的邮政编码来获取国家名称?
我查看了 Core Location Framework,但通过给定邮政编码并查找国家/地区名称,它看起来并没有反过来工作。
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.
我希望 iOS SDK 上有 class,我真的不想使用其中一个 Google Maps API
是的,您的解决方案可以在 iOS SDK 中找到。
将文本字段连接到此操作:
- (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]);
}
}
}];
}
我不知道 iOS 是否支持所有国家/地区的邮政编码,但它绝对适用于英国(例如 "YO258UH" 的邮政编码)和加拿大 ("V3H5H1")
Michael Dautermann's 答案是正确的,只需为 swift (v4.2) 添加一个代码,如果有人来此 post 寻找它:
@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
}
}
}
}
}
是否可以通过提供用户的邮政编码来获取国家名称?
我查看了 Core Location Framework,但通过给定邮政编码并查找国家/地区名称,它看起来并没有反过来工作。
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.
我希望 iOS SDK 上有 class,我真的不想使用其中一个 Google Maps API
是的,您的解决方案可以在 iOS SDK 中找到。
将文本字段连接到此操作:
- (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]);
}
}
}];
}
我不知道 iOS 是否支持所有国家/地区的邮政编码,但它绝对适用于英国(例如 "YO258UH" 的邮政编码)和加拿大 ("V3H5H1")
Michael Dautermann's 答案是正确的,只需为 swift (v4.2) 添加一个代码,如果有人来此 post 寻找它:
@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
}
}
}
}
}