如何在iPhone应用程序中找到城市名称?

时间:2012-06-20 13:23:27

标签: iphone objective-c json google-maps mapkit

如何在google api中找到iPhone SDK中的城市名称?

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

1 个答案:

答案 0 :(得分:3)

首先,您在this link创建google api密钥,然后将其插入下面的代码中。

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&sensor=true&key=<your api key here>",your_textField_search_str]]];
[request setHTTPMethod:@"POST"];

NSError *error;
NSURLResponse *response;
NSData *returnData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *returnString=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);  
SBJSON *json = [[SBJSON new] autorelease];
dic = [[NSMutableDictionary alloc] initWithDictionary:[json objectWithString:returnString error:nil]];