想要从邮政编码中获取州名

时间:2012-03-12 07:12:28

标签: iphone objective-c ios xcode

我正在制作一个带有邮政编码字段的iphone应用程序。当用户输入邮政编码时,我想从邮政编码中获取州名,是否有人可以建议我。

3 个答案:

答案 0 :(得分:3)

您可以使用Yahoo的API,如:

 http://where.yahooapis.com/geocode?q=77048&appid=0
  

其中q = zipCode

答案 1 :(得分:3)

您可以像这样使用Geocoding API

http://maps.googleapis.com/maps/api/geocode/json?address=77048&sensor=true

其中地址是您的邮政编码。您可以查看地理编码API的here

答案 2 :(得分:1)

嗨,最后我已经完成了google api的帮助。也很长时间拉了我的头发后得到了解决方案。

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