简体   繁体   English

Android中的Google Directions API

[英]Google Directions API in Android

In my Android application I use the Directions API to calculate the traveltime to location A to B by car. 在我的Android应用程序中,我使用Directions API来计算汽车到位置A到B的旅行时间。

Now I am trying to build a new feature using public transportation and with the Directions API you can do this by adding &mode=transit . 现在,我正在尝试使用公共交通工具构建新功能,并通过Directions API可以通过添加&mode=transit After I done this I get an error saying I have to use an API key. 完成此操作后,我收到一条错误消息,要求我必须使用API​​密钥。 So I added my Android application API key to my code using &key=*** . 因此,我使用&key=***将Android应用程序API密钥添加到了代码中。 Unfortunately my application gives the following response: 不幸的是,我的应用程序给出以下响应:

"This IP, site or mobile application is not authorized to use this API key." “此IP,站点或移动应用程序无权使用此API密钥。”

At my Android application API key the right fingerprint is there. 在我的Android应用程序API密钥处,正确的指纹在那里。

Then I thought it is a HTTP request so am I not supposed to use the "Key for browser applications". 然后,我认为这是一个HTTP请求,因此我不应该使用“浏览器应用程序的密钥”。 So I went and tried that but then I get the same error. 所以我去尝试了一下,但是后来我得到了同样的错误。 While Referers says: "Any referer allowed". 而“引荐来源”说:“允许任何引荐来源”。

My code: 我的代码:

AsyncHttpClient client = new AsyncHttpClient();
client.get("https://maps.googleapis.com/maps/api/distancematrix/json?origins="+ latlong +"&destinations="+ location +"&key=***&sensor=true&units=metric&language=nl&mode=" + mode, new AsyncHttpResponseHandler() {

    @Override
    public void onStart() {}

    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] response) {}

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {}

}

Here is the JSON response: 这是JSON响应:

{
    "destination_addresses" : [],
    "error_message" : "This IP, site or mobile application is not authorized to use this API key.",
    "origin_addresses" : [],
    "rows" : [],
    "status" : "REQUEST_DENIED"
}

So which key am I supposed to use and how can I get this to work properly? 那么,我应该使用哪个密钥?如何使它正常工作?

Thanks in advance! 提前致谢!

You may need to enable the API in the developer's console: 您可能需要在开发者控制台中启用API:

https://console.developers.google.com/ https://console.developers.google.com/

Go to "Select a Project", choose your project, click "APIs and Auth" on the left, then "APIs", then under the listing of Google Maps APIs, select the correct one (it should be Google Maps Directions API in this case) and finally, click Enable API. 转到“选择一个项目”,选择您的项目,单击左侧的“ API和身份验证”,然后单击“ API”,然后在Google Maps API列表下,选择正确的一个(在此应为Google Maps Directions API大小写),最后,单击启用API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM