【问题标题】:How to call Google Distance api from android app using Android key?如何使用 Android 密钥从 android 应用程序调用 Google Distance api?
【发布时间】:2015-09-04 06:32:40
【问题描述】:

我们正在使用谷歌距离矩阵 API 来显示用户当前位置和目的地点之间的旅行距离,该活动也显示谷歌地图。我们正在使用以下 url 以及使用 app 包和 debug.keystore 的 sha1 生成的 Android 密钥。

// 路由的起源 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 键。

【问题讨论】:

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


    【解决方案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;
    }
    

    【讨论】:

    • 感谢您的帮助。但是对于发布来说,不使用 API KEY 是可以的。
    • 您收到错误消息,因为缺少您的 API 控制台再次检查所有服务以及使用您的服务
    【解决方案2】:

    API_KEY 在使用各种 Google Maps API 时不是必需的。我在 Advanced REST Client 中测试了没有 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,您也可以获得成功的响应。根据official documentation 此密钥标识您的应用程序以用于配额管理。因此,如果您能够支持非常大的用户群,您应该使用 API_KEY 并请求额外的配额。

    【讨论】:

    • 如果我们能够支持庞大的用户群,那么我们应该怎么做?添加 API 密钥将响应错误消息..!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    相关资源
    最近更新 更多