Want to get state name from zip code

I am making an iphone application that has a zip. When the user enters the zip code, I want to get the state name from the zip code, is that possible if someone suggests me.


source to share

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];




You can use the Yahoo API, for example:



where q = zipCode



You can use the geocoding API for this type


where address is your zipcode. You can see here for the Geocoding API.



All Articles