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

时间:2015-09-04 06:32:40

标签: android google-maps google-maps-android-api-2

我们正在使用Google Distance Matrix API在显示Google地图的活动中显示用户当前位置和目标点之间的行程距离。我们正在使用以下url和使用app package生成的Android密钥以及debug.keystore的sha1。

//路线的起源         String 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";

但上面的网址给出了以下回复,

{"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密钥。

2 个答案:

答案 0 :(得分:-1)

使用这种方式可能对此答案很有帮助

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;
}

答案 1 :(得分:-2)

使用各种Google Maps API时,API_KEY不是必需的要求。我在高级REST客户端中测试了没有API密钥的距离Matrix API的REST 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_KEY,您也可以获得成功的响应。根据{{​​3}} 此密钥标识您的应用程序以进行配额管理。因此,如果您能够支持非常大的用户群,则应使用API​​_KEY并请求其他配额。