i am working on one application which requires the feature of adding the custom locations
like. location myhome, myschool etc..
for which i have used the following google api
https://developers.google.com/places/documentation/#adding_a_place
and post the data as required in api.
i have added the following code on clicking add location button.
-(IBAction)submitPressed:(id)sender
{
NSString *temp = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/add/json"];
temp = [temp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"add location to map url: %@",temp);
NSURL *url = [NSURL URLWithString:temp];
NSString *requeststr = @"sensor=false&key=AIzaSyCd4CHYWFPLhw4g5yGJyYZXSV-RZRIQfiM&location=-33.8669710/151.1958750&accuracy=50&name=Google Shoes!&types=shoe_store&language=en-AU";
requeststr = [requeststr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *requestdata = [NSData dataWithBytes:[requeststr UTF8String] length:[requeststr length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestdata];
NSURLResponse *response = NULL;
NSError *requestError = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"responseString:%@",responseString);
}
when i check log for responseString it shows me
`<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 400 (Bad Request)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}
</style>
<a href=//www.google.com/><img src=//www.google.com/images/errors/logo_sm.gif alt=Google></a>
<p><b>400.</b> <ins>That’s an error.</ins>
<p>Your client has issued a malformed or illegal request. <ins>That’s all we know.</ins>
so, can anyone please help me to find out the mistake or any suggestion for add location to map. i have tried different key in calling the api but everytime showing the same error.
thanks in advance.
The code is actually slightly more complicated and lengthier than that. I won’t provide all the code so I will point you to a GREAT tutorial with its sample code (only works on iOS Device not on the simulator by the way):
Tutorial On Google Places for iOS SDK
Tutorials AMAZING Sample Code Project For Download
Also just so you know, the reason the output is coming with weird characters is because the response string you are getting is in the form of XML. The project uses JSON however. You will have to parse the XML using
NSXMLParserbut the project I linked you to has a comprehensive tutorial and the sample project will impress you!