我有以下代码发出Google Places API请求。此时参数是静态设置的。我将如何制作这些参数(类型和lat / lon以及Google Key - 我在.h文件中定义为常量)对象呢?
我的问题出现在NSURL上,因为我无法为其添加格式说明符。
感谢您的帮助。
-(void)ParseXML_of_Google_PlacesAPI
{
NSURL *googlePlacesURL = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778&radius=500&types=bar&sensor=false&key=MyGoogleAPIKey"];
NSData *xmlData = [NSData dataWithContentsOfURL:googlePlacesURL];
xmlDocument = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:nil];
NSArray *arr = [xmlDocument.rootElement elementsForName:@"result"];
for(GDataXMLElement *e in arr )
{
[placesOutputArray addObject:e];
}
答案 0 :(得分:4)
Good ol'stringWithFormat
?
`NSString* urlToCall = [NSString stringWithFormat:@"http:://url.to.webservice/api?param1=%@¶m2=%@", param1, param2]`
答案 1 :(得分:2)
这可能对你有所帮助
float lat=34.0522222,lon=-118.2427778;
NSString *typestr=@"bar";
NSString *key=@"MyGoogleAPIKey";
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f&radius=500&types=%@&sensor=false&key=%@",lat,lon,typestr,key]];
NSLog(@"url values ==%@",url);
答案 2 :(得分:1)
直接向NSURL格式化说明符,如下所示:
NSURL *googlePlacesURL = [NSURL URLWithString:[NSString stringWithFormat:@"http:://url.to.webservice/api?param1=%@%param2=%@", param1, param2]];