var customerLocation = '1903 Toro Canyon Rd, Austin, TX 78746'; var store1 = '3808 W. 35th, Austin, TX 78703'; var store2 = '4933 Plaza on the Lake, Austin, TX 78746'; var store3 = '6500 Bee Cave Rd, Austin, TX 78746';
function calculateDistances() { // Distance Matrix サービス オブジェクトを新規作成 var service = new google.maps.DistanceMatrixService(); // 事前に定義した出発地点と目的地、および // 交通情報に基づく所要時間を使用するかしないかのオプションを // 設定 service.getDistanceMatrix({ origins: [customerLocation], destinations: [store1, store2, store3], travelMode: google.maps.TravelMode.DRIVING, unitSystem: google.maps.UnitSystem.IMPERIAL, avoidHighways: false, avoidTolls: false, durationInTraffic: true }, callback); } function callback(response, status) { if (status != google.maps.DistanceMatrixStatus.OK) { console.log('DistanceMatrix Error: ', status); } else { // 出発地点と目的地の配列を取得 var origins = response.originAddresses; var destinations = response.destinationAddresses; for (var i = 0; i < origins.length; i++) { // 各出発地点について、目的地の距離と所要時間の結果を // 取得 var results = response.rows[i].elements; for (var j = 0; j < results.length; j++) { // 後でソートできるよう結果を保存 storeResults.push([destinations[j], results[j].duration_in_traffic.value, results[j].distance.value]); } } // 交通情報に基づく所要時間で結果をソート storeResults.sort(function(a, b) { return a[1] - b[1]; }); } }
"destination_addresses" : [ "3808 West 35th Street, Austin, TX 78703, USA", "4933 Plaza on the Lake, Austin, TX 78746, USA", "6500 Bee Cave Road, Austin, TX 78746, USA" ], "origin_addresses" : [ "1903 Toro Canyon Road, Austin, TX 78746, USA" ], "rows" : [ { "elements" : [ { "distance" : { "text" : "5.4 mi", "value" : 8631 }, "duration" : { "text" : "15 mins", "value" : 917 }, "duration_in_traffic" : { "text" : "20 mins", "value" : 1188 }, "status" : "OK" }, { "distance" : { "text" : "4.4 mi", "value" : 7157 }, "duration" : { "text" : "9 mins", "value" : 569 }, "duration_in_traffic" : { "text" : "15 mins", "value" : 911 }, "status" : "OK" }, { "distance" : { "text" : "4.7 mi", "value" : 7490 }, "duration" : { "text" : "11 mins", "value" : 635 }, "duration_in_traffic" : { "text" : "11 mins", "value" : 635 }, "status" : "OK" } ] }
0 件のコメント :
コメントを投稿