2013-03-19 68 views
2

我是IOS新手。我必須向Google地方信息添加新地點。我已經提到這個鏈接https://developers.google.com/places/documentation/actions添加按鈕點擊的地方,但我很困惑有關爲此傳遞參數。向Google Places添加新地點

我的編碼線路都是這樣來獲取:

NSString *lat [email protected]"-33.8669710"; 
NSString *longt [email protected]"151.1957362"; 
gKey = @"my api key"; 

NSString *placeString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/add/json?sensor=false&key=%@HTTP/1.1Host:maps.googleapis.com {\"location\":{\"lat\":%@,\"lng\":%@},\"accuracy\": 50,\"name\":\"Gimmy Pet Store!\",\"types\":[\"pet_store\"],\"language\":\"en-AU\"}",gKey,lat,longt]; 

placeString = [placeString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSLog(@"Main Place Url: %@",placeString); 
NSURL *placeURL = [NSURL URLWithString:placeString]; 

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:placeURL]; 

[request setHTTPMethod:@"POST"]; 

NSURLConnection *placesConn =[[NSURLConnection alloc] initWithRequest:request delegate:self]; 
+0

@iPatel Google Places – Kalyani 2013-03-19 07:05:59

+0

我認爲你應該在你的示例代碼下面提出確切的問題。究竟哪個參數或方法令你感到困惑? – 2013-03-19 07:23:22

+0

我很困惑,如何將參數傳遞給需要Post方法格式的鏈接。 – Kalyani 2013-03-19 09:58:16

回答

3

我做了以下修改我的代碼&終於被成功執行...

//for setting Parameters to post the url.just change tag values to below line... 

NSString *str1 = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><PlaceAddRequest><location><lat>your latitude</lat><lng>your longitude</lng></location><accuracy>50</accuracy><name>place name</name><type>supported type</type><language>en-US</language></PlaceAddRequest>"]; 
NSLog(@"str1=====%@",str1); 

NSString *str2 = [str1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

NSData *requestdata = [NSData dataWithBytes:[str2 UTF8String] length:[str2 length]]; 
NSString *postLength = [NSString stringWithFormat:@"%d", [requestdata length]]; 

//requesting main url to add new place to google places 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/add/xml?sensor=false&key=your api key"]]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 

[request setHTTPMethod:@"POST"]; 
[request setHTTPBody:[NSData dataWithBytes:[str1 UTF8String] length:[str1 length]]]; 

//NSURLConnection *placesConn =[[NSURLConnection alloc] initWithRequest:request delegate:self]; 
NSData *returndata = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *returnstr = [[[NSString alloc] initWithData:returndata encoding:NSUTF8StringEncoding] autorelease]; 
NSLog(@"returnstr: %@",returnstr); 

&的話,我已經破譯回報迴應,我得到我狀態確定...... :)

任何人都可以使用上面的代碼...如果有任何幫助需要你可以肯定請問.... :)

相關問題