1

在我的 iPhone 应用程序中,我使用的是 Google API。我必须做的一件事是我有一个国家的下拉列表,当我选择任何国家时,我需要获取该国家的所有城市。是否可以使用 Google API,或者我必须使用任何其他 API。请建议!谢谢-

4

1 回答 1

2

google Geocoder API 返回 JSON ,它只是一种使用 GET 方法的网络服务

这是 Google Gecoder API,这是Web 服务的链接,在此链接中,我将区域名称指定为 london。你可以给你国名

注意:您需要在代码中包含 SBJson 库。

在该链接的末尾,我附加了地址,如果附加地址,则需要提供区域名称(或),如果附加纬度和经度,则需要提供坐标,它将相应地返回结果。

而调用google api的代码会是这样的

 //Call the Google API
 NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
 NSLog(@"The get address is %@", req);
 //Pass the string to the NSURL
 NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
 NSLog(@"The result is %@", result);
 //Initialize the SBJSON Class
 SBJSON *parser = [[SBJSON alloc] init];
 NSError *error = nil;
 //Get the resullts and stored in the address array
 addressArray = [parser objectWithString:result error:&error];
 //Get the latitude values for the given address
 NSDictionary *dict = [[[addressArray valueForKey:@"results"] valueForKey:@"geometry"] valueForKey:@"location"];
 self.latitudeValue = [[dict valueForKey:@"lat"] objectAtIndex:0];
 self.longitudeValue = [[dict valueForKey:@"lng"] objectAtIndex:0];
 NSLog(@"LAT : %@",self.latitudeValue);
 NSLog(@"LONG : %@",self.longitudeValue);

我刚刚给出了一个想法,您可以在那里的 JSON 响应中以短名称、长名称等形式获取所有名称

于 2012-05-22T14:26:15.293 回答