在iOS中从邮政编码自动填充城市和州 [英] Autofill city and state from zip code in iOS
本文介绍了在iOS中从邮政编码自动填充城市和州的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
1.邮编,2.城市和3.州。
如何在iOS中自动填充城市和州字段?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString * currentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
int length = [currentString length];
if(length> 5)
{
return NO;
}
if(length == 5)
{
[self getCityAndState];
}
返回YES;
}
- (void)getCityAndState
{
//如何使用google(或任何)api自动填充city和state在objective - c中?
解决方案使用
Google GeoCoding API
来提取信息,如果您想发送邮政编码以接收其他信息,请使用以下命令: NSString * strRequestParams = [NSString stringWithFormat:@http://maps.googleapis.com/maps/api/geocode/json?address=&components=postal_code:%@&sensor=false,邮政编码];
strRequestParams = [strRequestParams stringByAddingPercentEscapesUsingEncoding:NSStringEncodingConversionExternalRepresentation];
NSURL * url = [NSURL URLWithString:strRequestParams];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@GET];
NSError *错误;
NSURLResponse *响应;
NSData * responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:& response error:& error];
if(!response){
//Connection Error,Failed to Connect to Internet
}
NSString * respString = [[NSString alloc ] initWithData:responseData encoding:NSUTF8StringEncoding];
// NSLog(@RECEIVED DATA:%@,respString);
如果您的zipcode变量为32000,您将获得这个 JSON结果:
您可以解析此json以提取您想要的任何信息,包括国家,城市,经度,纬度等。
I have three textfields in my view. 1. Zip code, 2. City and 3. State.
How to autofill city and state field from the zip code in iOS?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *currentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
int length = [currentString length];
if(length > 5)
{
return NO;
}
if(length == 5)
{
[self getCityAndState];
}
return YES;
}
- (void) getCityAndState
{
//How to use google (or any) api to autofill city and state in objective - c?
}
解决方案
Use the Google GeoCoding API
to extract Information, if you want to send zip code to receive other information, use this:
NSString *strRequestParams = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=&components=postal_code:%@&sensor=false",zipCode];
strRequestParams = [strRequestParams stringByAddingPercentEscapesUsingEncoding:NSStringEncodingConversionExternalRepresentation];
NSURL *url = [NSURL URLWithString:strRequestParams];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
NSError *error;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!response) {
// "Connection Error", "Failed to Connect to the Internet"
}
NSString *respString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ;
//NSLog(@"RECEIVED DATA : %@", respString);
If your zipcode variable is 32000, you will get the this JSON result:
You can parse this json to extract any information you want including Country, City, longitude, latitude etc
这篇关于在iOS中从邮政编码自动填充城市和州的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文