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() { // Create a new Distance Matrix Service object var service = new google.maps.DistanceMatrixService(); // Set the options such as the pre-defined origin // and destinations, as well as specifying to use // duration in traffic 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 { // Get the arrays of origins and destinations var origins = response.originAddresses; var destinations = response.destinationAddresses; for (var i = 0; i < origins.length; i++) { // For each of the origins, get the results of the // distance and duration of the destinations var results = response.rows[i].elements; for (var j = 0; j < results.length; j++) { // Store the results for later sorting storeResults.push([destinations[j], results[j].duration_in_traffic.value, results[j].distance.value]); } } // Sort the results by duration in traffic 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" } ] }
Give us feedback in our Product Forums.