如何使用android密钥从android应用程序调用Google Distance api?

如何使用android密钥从android应用程序调用Google Distance api?,android,google-maps,google-maps-android-api-2,Android,Google Maps,Google Maps Android Api 2,我们正在使用Google Distance Matrix API在一个显示Google地图的活动中显示用户当前位置和目的地之间的旅行距离。我们使用下面的url和使用app包和debug.keystore的sha1生成的Android密钥 //路线起点 字符串str_origin=“origin=“+origin.latitude+”,“+origin.longitude // Destination of route String str_dest = "destination=

我们正在使用Google Distance Matrix API在一个显示Google地图的活动中显示用户当前位置和目的地之间的旅行距离。我们使用下面的url和使用app包和debug.keystore的sha1生成的Android密钥

//路线起点 字符串str_origin=“origin=“+origin.latitude+”,“+origin.longitude

    // Destination of route
    String str_dest = "destination=" + dest.latitude + "," + dest.longitude;

    // Building the parameters to the web service
    String parameters = str_origin + "&" + str_dest + "&" + sensor;

    // String parameters = str_origin + "&" + str_dest + "&";
    // Output format
    String output = "json";

String url = "https://maps.googleapis.com/maps/api/distancematrix/" + output +"?"
                + parameters + "&mode=walking&key=" +"Android_Key";
但上面的url给出了以下回应

{"destination_addresses" : [],   "error_message" : "This IP, site or mobile application is not authorized to use this API key. Request received from IP address *.*.*.*, with empty referer",   "origin_addresses" : [],   "rows" : [],   "status" : "REQUEST_DENIED"}

我在这里做错了什么?它与未映射到应用程序包名称和sha1的android密钥一起工作。

使用这种方式可能会有助于回答这个问题

private String getDirectionsUrl(LatLng origin, LatLng dest) {

    // Origin of route
    String str_origin = "origin=" + origin.latitude + ","
            + origin.longitude;

    // Destination of route
    String str_dest = "destination=" + dest.latitude + "," + dest.longitude;

    // Sensor enabled
    String sensor = "sensor=false";

    // Building the parameters to the web service
    String parameters = str_origin + "&" + str_dest + "&" + sensor;

    // Output format
    String output = "json";

    // Building the url to the web service
    String url = "https://maps.googleapis.com/maps/api/directions/"
            + output + "?" + parameters;

    return url;
}

使用各种Google Maps API时,API_密钥不是必需的要求。我在高级REST客户机中测试了距离矩阵API的REST API调用,没有API密钥,并且成功地收到了200 OK响应

请求如下:

 https://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling
以下是回应:

{
   "destination_addresses" : [ "San Francisco, CA, USA", "Victoria, BC, Canada" ],
   "origin_addresses" : [ "Vancouver, BC, Canada", "Seattle, WA, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1,706 km",
                  "value" : 1705627
               },
               "duration" : {
                  "text" : "3 days 19 hours",
                  "value" : 327067
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "139 km",
                  "value" : 138549
               },
               "duration" : {
                  "text" : "6 hours 42 mins",
                  "value" : 24146
               },
               "status" : "OK"
            }
         ]
      },
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1,449 km",
                  "value" : 1449084
               },
               "duration" : {
                  "text" : "3 days 4 hours",
                  "value" : 274649
               },
               "status" : "OK"
            },
            {
               "distance" : {
                  "text" : "146 km",
                  "value" : 146496
               },
               "duration" : {
                  "text" : "2 hours 52 mins",
                  "value" : 10324
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

这意味着,即使您没有在请求调用中放入API_密钥,您也可以获得成功的响应。根据此键识别您的应用程序,以便进行配额管理。因此,如果您能够支持非常大的用户群,那么应该使用API_密钥并请求额外的配额

谢谢你的帮助。但对于发布版,不使用API密钥可以吗?您会收到错误消息,因为您的API控制台丢失了一些东西。请再次检查所有服务,并检查哪些使用您。如果我们能够支持大量用户群,我们该怎么办?添加API密钥将响应一条错误消息。。!