使用地方报告请求(添加地点)api将位置添加到谷歌地图

时间:2012-07-02 13:02:21

标签: iphone objective-c google-maps-api-3

我正在处理一个需要添加自定义位置功能的应用程序 喜欢。位置myhome,myschool等..  为此,我使用了以下谷歌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时尝试了不同的键,但每次都显示相同的错误。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

代码实际上比这更复杂,更长。我不会提供所有代码,因此我将指向一个带有示例代码的GREAT教程(仅适用于iOS设备而不是模拟器上的代码):

Tutorial On Google Places for iOS SDK

Tutorials AMAZING Sample Code Project For Download

另外,您知道,输出带有奇怪字符的原因是因为您获得的响应字符串是XML格式。该项目使用JSON。您将不得不使用NSXMLParser解析XML,但我链接到您的项目有一个全面的教程,示例项目会给您留下深刻的印象!