2012-09-05 88 views
5

我已经做到了获取状态“REQUEST_DENIED”从谷歌获取数据后,会将API

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=30.722322,76.76126&radius=500&types=food&sensor=true&key=any_apiKey"]]; 

    NSURLResponse *response; 
    NSError *error; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    NSString *strResponse = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@",strResponse); 

    SBJSON *sbJason = [[SBJSON alloc] init]; 
    NSMutableDictionary *getPlaceList = [sbJason objectWithString:strResponse]; 

//但是我得到这个 -

{ 
"html_attributions" : [], 
    "results" : [], 
"status" : "REQUEST_DENIED" 
} 

**有没有用API密钥的任何问题我已经写了谷歌地图给出的API密钥。是否有与API密钥或者是什么,请告诉我这里是link of google api

+0

你真的把“any_apikey”作为你的api关键字吗? –

+0

不,不,它不是我真正的API密钥。我有谷歌给出不同的API密钥 –

+0

好,好:)只是想检查 –

回答

2

静态地图API和Places API的每个人都需要的API控制台该密钥在被启用。打开API控制台并启用对Places API的访问。礼遇限制:每天1,000个请求,之后您可能需要启用结算。

https://code.google.com/apis/console/

+0

嗨,如何在控制台中启用对Places API的访问。你能告诉我这些步骤吗? – Dave

1

试试这个什么问题: -

https://developers.google.com/places/documentation/

阅读下面的部分,并与步骤获得密钥进行。

身份验证

Google Places API使用API​​密钥来标识您的应用程序。 API密钥通过Google API控制台进行管理。在开始使用API​​之前,您需要自己的API密钥。要激活Places API的创造你的关键:

Visit the APIs console at https://code.google.com/apis/console and log in with your Google Account. 
A default project called API Project is created for you when you first log in to the console. You can use the project, or create a new one by clicking the API Project button at the top of the window and selecting Create. Maps API for Business customers must use the API project created for them as part of their Places for Business purchase. 
Click the Services link from the left-hand menu. 
Click the Status switch next to the Places API entry. The switch slides to On. 
Click API access from the left navigation. Your key is listed in the Simple API Access section. 
+0

LOL ...我有那个API密钥AIzaSyC6RgmTP5LoaS6oFSiOSu ????????????但 我表明any_apiKey出于安全目的... –

+0

然后最新的问题是什么? – Gypsa

+0

您需要阅读整个文档并解决问题,因为在不知道api键的情况下,我们无法运行url并检查是否存在错误。 – Gypsa

4

尝试使用浏览应用程序键,而不是iOS的关键。你可以在这里找到:在左

+0

BROWSER应用程序密钥工作,但不是iOS密钥.... – Maulik

+0

我试图使用Android应用程序的密钥,但@CaptainQuality说,使用浏览器应用程序密钥为我工作,不要创建您的移动操作系统特定密钥。我不确定为什么Google需要这么困惑。 – Rajaraman

20

尽管已经回答了这个问题,但我认为社区可以在解释ACTUALLY需要完成的工作中做得更好,才能实现这一目标。

我正在撕裂我的头发这件事,它只是对我没有意义..我正在做一个iOS/Android应用程序,所以我做了一个iOS/Android密钥... 错了。

使用Google的Places API,甚至不考虑您的包标识符。

你真正想要做的是这样的: (我使用新的用户界面)

1.登录到https://cloud.google.com/console#/project

选择你的项目名称,然后进入API的&认证> API

请确保您已打开Places API。这是Places-API需要启用的唯一工作。 enter image description here

2.进入凭据

单击创建下公共API访问 enter image description here

3.选择浏览KEY enter image description here

4.单击创建新的关键,别无他法

将HTTP Refer框留空。

enter image description here

5.使用这里

此密钥生成将允许通过您的开发人员登录的任何设备访问API的任何用户的关键。 你可以在这里尝试一下:(一定要更换YOUR_KEY_HERE与你产生的密钥)

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Food%20Sh&sensor=false&radius=500&location=0,0&key=YOUR_KEY_HERE 

6.享受

现在你可以使用上面你的Android/iOS设备是URL 。

+0

就是这样。第4步(将HTTP Refer栏留空。)为我工作。 – Marco

+0

这是最好的答案。非常感谢。 –

+0

虽然这可能感觉像一个kludgey答案,但它是唯一的选择。 Google Places API目前不支持Android或IOS密钥.... SMH您可以通过google https://code.google.com/p/gmaps-api-issues/issues/detail?id=查看官方要求。 4896 –

0

我结束了这段代码。

UIActivityIndicatorView *activity=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
activity.center=self.view.center; 
[activity startAnimating]; 
[self.view addSubview:activity]; 


NSString *str=[[NSString alloc] init]; 
str=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/textsearch/json?query=%@&sensor=true&key=YOUR_BROWSER_KEY",searchQuery]; 
NSMutableURLRequest *request1=[[NSMutableURLRequest alloc] init]; 
NSURL *url= [[NSURL alloc] initWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
[request1 setURL:url]; 
[request1 setHTTPMethod:@"GET"]; 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

    NSError *requestError = nil; 
    NSURLResponse *urlResponse = nil; 
    NSData *response1 = 
    [NSURLConnection sendSynchronousRequest:request1 
          returningResponse:&urlResponse error:&requestError]; 


    dispatch_async(dispatch_get_main_queue(), ^{ 


     if ([activity isAnimating]) { 
      [activity startAnimating]; 
      [activity removeFromSuperview]; 
     } 
     NSError* error; 
     NSDictionary* json = [NSJSONSerialization JSONObjectWithData:response1 
                  options:kNilOptions 
                   error:&error]; 

     NSLog(@"%@",json); 

     self.searchArray=[json objectForKey:@"results"]; 
     [self.tableView setDataSource:self]; 
     [self.tableView setDelegate:self]; 
     [self.tableView reloadData]; 

     [self showPins:self.searchArray]; 

    }); 
});