是否可以通过提供用户的邮政编码来获取国家/地区名称?
我查看了核心位置框架,但考虑到邮政编码并找到国家名称,它看起来并没有相反的方式。
核心位置框架(CoreLocation.framework)提供位置 和标题信息到应用程序。有关位置信息,请参阅 框架使用板载GPS,小区或Wi-Fi无线电来查找 用户当前的经度和纬度。
我希望iOS SDK上有一个课程,我真的不想使用其中一个Google Maps API
答案 0 :(得分:3)
是的,您的解决方案可以在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是否支持所有国家/地区的邮政编码,但它绝对适用于英国(例如邮政编码为#34; YO258UH")和加拿大(" V3H5H1&#34) ;)
答案 1 :(得分:0)
Michael Dautermann's的答案是正确的,如果有人在这篇帖子中寻找它,只需添加一个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
}
}
}
}
}