简体   繁体   中英

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.

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 . After I done this I get an error saying I have to use an API key. So I added my Android application API key to my code using &key=*** . Unfortunately my application gives the following response:

"This IP, site or mobile application is not authorized to use this API key."

At my Android application API key the right fingerprint is there.

Then I thought it is a HTTP request so am I not supposed to use the "Key for browser applications". 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:

{
    "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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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