Reputation: 65
In my iPhone app I am using Google API. One thing I have to do is I have a drop down of countries and when I select any country, i need to fetch all the cities of that country. Is it possible with Google API or I have to use any other. Please suggest! Thanks-
Upvotes: 1
Views: 1396
Reputation: 4946
The google Geocoder API returns JSON , It is just a kind of a web service which uses GET method
And This is the Google Gecoder API and This is the link for that web service and in this link i have given the region name as london. you can give you country name
Note: You need to include SBJson library to your code.
At the end of that link i have appended address, if you append address- you need to give the region name (or) if you append latitude and longitude, you need to give coordinates and it will return the results accordingly.
And the code for calling google api will be like this
//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);
I have just given an idea, you can get all the names in form of Short name, Long name etc in the JSON Response there
Upvotes: 2