Want to get state name from zip code
3 answers
Hi finally i did it using google api. Also after a long time pulling the hair, I got a solution.
str_StateName = @"";
NSString *address = [NSString stringWithFormat:@"United States,%@,%@",txt_City.text,txt_ZipCode.text];
NSString *urlString1 = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true",
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSError *error = nil;
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString1] encoding:NSUTF8StringEncoding error:&error];
NSArray *listItems = [locationString componentsSeparatedByString:@"\""];
//NSArray *names = [listItems valueForKeyPath:@"long_name"];
for (i = 0; i < [listItems count]; i++)
{
if ([[listItems objectAtIndex:i] isEqualToString:@"administrative_area_level_1"])
{
NSLog(@"[listItems objectAtIndex:%d] %@",i,[listItems objectAtIndex:i-8]);
str_StateName = [NSString stringWithFormat:@"%@",[listItems objectAtIndex:i-8]];
}
}
if([str_StateName isEqualToString:@""])
{
alertView = [[ UIAlertView alloc] initWithTitle:@"Warning" message:@"Please enter correct zip code and city." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
return;
}
+1
source to share
You can use the geocoding API for this type
http://maps.googleapis.com/maps/api/geocode/json?address=77048&sensor=true
where address is your zipcode. You can see here for the Geocoding API.
+3
source to share