-1

我正在开发一个需要添加自定义位置功能的应用程序。位置 myhome、myschool 等。为此我使用了以下 google api https://developers.google.com/places/documentation/#adding_a_place

并在 api 中按要求发布数据。我在单击添加位置按钮时添加了以下代码。

-(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);
}

当我检查日志中的 responseString 时,它显示了我

`<!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>

所以,任何人都可以帮我找出错误或将位置添加到地图的任何建议。我在调用 api 时尝试了不同的键,但每次都显示相同的错误。提前致谢。

4

1 回答 1

0

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 NSXMLParser but the project I linked you to has a comprehensive tutorial and the sample project will impress you!

于 2012-07-02T13:08:50.533 回答