2009-06-25 39 views
30

我使用谷歌地圖API遇到的所有例子似乎都顯示了某種地圖。我希望在您向A站點詢問道路描述時,將他們提供給您的汽車估計行駛時間的數據納入到站點中。只有那些數據。沒有爲最終訪問者加載地圖是否可能?使用谷歌地圖API獲取旅行時間數據

回答

18

是的,這絕對可以使用API​​。您可以構造一個沒有地圖或方向div的GDirections對象。您可以從A到B執行加載請求,然後致電getDuration方法以獲取總行程時間。

首先,你需要創建一個方向對象:

// no need to pass map or results div since 
// we are only interested in travel time. 
var directions = new GDirections(); 

然後做您的負載要求,以解決方向(我用了兩個緯度和經度作爲我的起點和終點,但你可以在這裏使用的地址以及):

var wp = new Array(); 
wp[0] = new GLatLng(32.742149,119.337218); 
wp[1] = new GLatLng(32.735347,119.328485); 
directions.loadFromWaypoints(wp); 

然後,你需要監聽負載事件,這樣你可以調用getDuration一旦方向已經得到解決:

GEvent.addListener(directions, "load", function() { 
    $('log').innerHTML = directions.getDuration().seconds + " seconds"; 
     }); 

您可以找到whole example hereJavaScript here。你可以檢查Google Maps Documentation瞭解更多關於你可以給GDirections對象的選項信息(比如步行距離等)。您還應該聽取錯誤事件的地理編碼失敗。

+0

現在我們假設我有一個有50個點的kml文件。我希望得到所有這些點之間的旅行時間,你應該怎麼做? – dassouki 2009-09-18 14:52:08

+0

該鏈接不起作用了! – Prachi 2016-05-05 11:48:13

+0

這似乎在理想的交通狀況下(忽略當前狀況)產生駕駛時間。無論出發時間如何,結果都是相同的。 – user3667349 2016-06-11 00:56:50

19

上述答案不正確 - 違反Google API服務條款。

Google Maps API只允許您計算旅行時間,如果它是針對顯示給用戶的Google地圖引用的。

如果您沒有向服務的最終用戶顯示Google地圖,則無法使用該API。

-3

僞代碼:

  1. DirectionsService .route({A,B})//得到這裏的DirectionsResult在

  2. 檢查距離和持續時間(行程時間)DirectionsLeg .//from的DirectionsResult .legs

    沒有航點的路線將包含一個DirectionsLeg,所以這就是你想要的。

18

根據記錄:在這個時候(2015年)GDirections對象,的GLatLng,GEVENT等都是棄用


您可以使用google.maps.DirectionsService class(谷歌地圖的JavaScript API V3)

在下面的代碼段,位置和目標是包含緯度和經度座標的對象。

1)google.maps.DirectionsService類承認LatLng和字符串值來提供其原始和目標屬性。
2)LatLng是緯度和經度的地理座標點。

var origin = new google.maps.LatLng(location.latitude, location.longitude); // using google.maps.LatLng class 
var destination = target.latitude + ', ' + target.longitude; // using string 

var directionsService = new google.maps.DirectionsService(); 
var request = { 
    origin: origin, // LatLng|string 
    destination: destination, // LatLng|string 
    travelMode: google.maps.DirectionsTravelMode.DRIVING 
}; 

directionsService.route(request, function(response, status) { 

    if (status === 'OK') { 
     var point = response.routes[ 0 ].legs[ 0 ]; 
     $('#travel_data').html('Estimated travel time: ' + point.duration.text + ' (' + point.distance.text + ')'); 
    } 
}); 
3

google.maps.DirectionsRenderer使用

距離:

使用
directionsDisplay.directions.routes[0].legs[0].distance.text 

時間:

directionsDisplay.directions.routes[0].legs[0].duration.text 
-2

您可以使用R包gmapsdistance。它確實是你想要的。它計算起源和目的地向量之間的距離/時間矩陣,併爲您提供一系列選項(運輸方式,出發時間等等)。

3
  1. 第一去這裏: https://developers.google.com/maps/documentation/distance-matrix/start 你需要:創建或選擇一個項目,啓用API,並得到一個API密鑰 只需點擊「一鍵搞定」,並假設你有一個Gmail帳戶,就能讓一切順利(或者在登錄到您的gmail帳戶後進入console.developer.google.com,並創建一個應用程序並啓用該API並在那裏獲取API密鑰)。
  2. 現在去https://developers.google.com/maps/documentation/distance-matrix/intro並按照細節。有幾件事需要注意:使用這些: & mode = driving & departure_time = now & traffic_model =悲觀如果您希望當前流量影響您的駕駛時間。我還使用&單位=帝國

在流量的持續時間僅返回如果以下所有條件都爲真:

該請求包括DEPARTURE_TIME參數。 該請求包含有效的API密鑰或有效的Google Maps API Premium計劃客戶端ID和簽名。 交通狀況可用於請求的路線。 模式參數設置爲駕駛。

是的,你必須顯示一個谷歌地圖的驅動時間「谷歌地圖距離矩陣API可能只能與在Google地圖上顯示結果一起使用。禁止使用谷歌地圖距離矩陣API數據不顯示谷歌地圖「
一定要結賬。 https://developers.google.com/maps/documentation/distance-matrix/usage-limitshttps://developers.google.com/maps/terms 對於T & C'S。

請注意,Mapquest不顯示基於實際流量的駕駛時間。

1

我在這個問題上苦苦掙扎了很長時間,但最終通過AJAX調用解決了它(確保你鏈接到了jQuery來做到這一點)。

//Pass parameters to a function so your user can choose their travel 
locations 

function getCommuteTime(departLocation, arriveLocation) { 
    //Put your API call here with the parameters passed into it 
    var queryURL = 
    "https://maps.googleapis.com/maps/api/distancematrix/json? 
    units=imperial&origins=" + departLocation + "&destinations=" + 
    arriveLocation + "&key=YOUR_API_KEY" 

    //Ajax retrieves data from the API 
    $.ajax({ 
    url: queryURL, 
    method: "GET" 
    }).then(function(response) { 

    //Navigate through the given object to the data you want (in this 
    case, commute time) 
    var commuteTime = response.rows[0].elements[0].duration.text; 

    console.log(commuteTime) 
    }); 

    } 


//This is where your user can give you the data you need 
$("#addRoute").on("click", function(event) { 

event.preventDefault(); 

var departLocation = $("#departInput").val().trim(); 
var arriveLocation = $("#arriveInput").val().trim(); 

//call the above function 
getCommuteTime(departLocation, arriveLocation); 

});