![](/img/trans.png)
[英]How to get country phone code for given country name in Swift iOS?
[英]How to get Country name by given Postal Code (ZIP) on iOS SDK
是否可以通過提供用戶的郵政編碼來獲取國家/地區名稱?
我查看了核心位置框架,但考慮到郵政編碼並找到國家名稱,它看起來並沒有相反的方式。
核心位置框架(CoreLocation.framework)為應用程序提供位置和標題信息。 對於位置信息,框架使用板載GPS,小區或Wi-Fi無線電來查找用戶當前的經度和緯度。
我希望在iOS SDK上有一個課程,我真的不想使用其中一個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”)
邁克爾·道特曼的回答是正確的,只要為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.