2015-09-04 39 views
0

我們正在使用Google Distance Matrix API來顯示用戶當前位置與目標點之間的旅行距離,以顯示Google地圖。我們正在使用以下url與使用應用程序包生成的Android密鑰和debug.keystore的sha1一起使用。如何使用Android密鑰從Android應用程序調用Google Distance api?

//路線起源 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的關鍵。

回答

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

謝謝你的幫助。但是發佈可以不使用API​​ KEY。 – Tejaswini

+1

您會收到錯誤消息,因爲有些東西缺少您的API控制檯再次檢查所有服務和使用您的服務 –

-1

API_KEY是不是必要的要求,同時使用各種谷歌地圖API。我在高級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在您的請求調用你就可以得到一個成功的迴應。根據official documentation此密鑰標識您的應用程序用於配額管理的目的。所以如果你能夠支持一個非常大的用戶羣,你應該使用API​​_KEY並請求額外的配額。

+0

如果我們能夠支持龐大的用戶羣,那麼我們應該怎麼做? 添加API密鑰將回復一條錯誤消息。 –

相關問題