2017-05-08 127 views
1

谷歌地圖API ..谷歌地圖API響應

我想要兩個地址之間的最短路徑。

以下是我的代碼。

它的工作正常,但它返回不必要的信息,這就是爲什麼url請求花費太多時間來獲得響應。

$source = "ClinRad Diagnostics & Research Centre, 725, CMH Road, Near CMH Hospital, Indiranagar 1st stage, Bengaluru, Karnataka 560038"; 

$destination = "HENNUR POLICE STATION, Balachandra Layout, Babusabpalya, Hennur Gardens, Bengaluru, Karnataka 560043"; 

$url ='http://maps.googleapis.com/maps/api/directions/json?origin='.urlencode($source).'&destination='.urlencode($destination).'&alternatives=true&sensor=false&mode=driving'; 

$data = file_get_contents($url); 
$response = json_decode($data); 
echo "<pre>"; 
print_r($response); 

效應初探陣列的詳細信息:https://jsfiddle.net/mbb5Lu32/

我要爭取在最短的路徑只有千米值..的事情其餘都是浪費我..

我怎樣才能像使用谷歌的具體信息地圖API?

+0

在所示響應7.8公里最短值.. –

+0

你能張貼JSON? –

+0

請檢查https://jsfiddle.net/mbb5Lu32/3/ –

回答

0

您可以使用Google distancematrix API。它僅返回最短路線的距離和持續時間。

要求:

https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=Washington,DC&destinations=New+York+City,NY&key= 

答案:

{ 
    "destination_addresses" : [ "New York City, New York, USA" ], 
    "origin_addresses" : [ "Washington, D.C., District of Columbia, USA" ], 
    "rows" : [ 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "362 km", 
        "value" : 361721 
       }, 
       "duration" : { 
        "text" : "3 Stunden, 47 Minuten", 
        "value" : 13640 
       }, 
       "status" : "OK" 
      } 
     ] 
     } 
    ], 
    "status" : "OK" 
} 
+0

進行計算,請將此結果與https://www.google.co.in/maps/ 比較,它會顯示錯誤的值 –

+0

好的,這是道路網絡距離的正確值。如果您想要通過步行或通過運輸路線獲得最短距離,則可以在api請求中設置旅行模式。 –

+0

嘗試比較這兩個地址: $ source =「M G Rd,KG Halli,Shanthala Nagar,Ashok Nagar,Bengaluru,Karnataka 560001」; $ destination =「36th Cross Rd,BDA Layout,Tilak Nagar,Jayanagar,Bengaluru,Karnataka 560041」; –