【问题标题】:Google distance metrix api returns REQUEST_DENIED谷歌距离metrix api返回REQUEST_DENIED
【发布时间】:2018-09-24 13:58:25
【问题描述】:

我正在使用 URLConnection 连接到 URL 以获取 android 中两个位置之间的距离和时间。每次它返回“REQUEST_DENIED”。我生成了没有任何限制的密钥,并启用了 Google Maps Android API、Google Maps Geocoding API 和 Google Places API Web Service。代码sn-p:

返回的响应是: { “目的地地址”:[], "error_message" : "此 API 项目无权使用此 API。", “origin_addresses”:[], “行”:[], “状态”:“REQUEST_DENIED” }

URL mapUrl = new URL("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York+City,NY&key=My_Key");
URLConnection connection = mapUrl.openConnection();
InputStream in = connection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(isw);
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

另请注意,当我在浏览器中粘贴相同的 URL 时,有时结果会显示在浏览器中。但不总是。如果它显示“REQUEST_Denter image description hereENIED”,此后它总是显示相同。我尝试再次禁用和启用 API 服务。第一次启用后,我在浏览器中得到了结果,但不是通过 android 应用程序。 我该如何解决这个问题?请求数也不是问题,因为它在限制范围内。

【问题讨论】:

标签: android google-maps android-studio google-places-api


【解决方案1】:
  1. 您只需要Distance Matrix API 的密钥(Google Maps Android API、Google Maps Geocoding API 和 Google Places API Web Service 还不够)。
  2. Distance Matrix API 中似乎存在错误:在您的请求中将 New+York+City,NY 替换为 New+York,NY 并且一切正常:

    URL mapUrl = new URL("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York,NY&key=My_Key");

会回复:

{
   "destination_addresses" : [ "New York, NY, USA" ],
   "origin_addresses" : [ "Washington, DC, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "225 mi",
                  "value" : 361954
               },
               "duration" : {
                  "text" : "3 hours 50 mins",
                  "value" : 13778
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多