Skip to content

Create the origin list properly for DistanceMatrixService()

Nel

Asked Aug 17, 2014, 1:28 PM EDT6 answers
Share
LinkedInX

Hello,

I have this code where I define the lists for origins and destinations in order to calculate the travel time between the pair. But the way I created the lists or something else is not ok, and this code doesn't perform the calculations.

Can anybody help me where I make a mistake?
Thanks 
C#
 origin = origin1 + "," + origin2 + "," + origin3;
        destin = destinationA + "," + destinationB + "," + destination;




alert(origin);

        alert(destin);
        function calculateDistances() {
        // for (var i = 0; i <= el.length; i++) {
            var service = new google.maps.DistanceMatrixService();
            service.getDistanceMatrix(
              {

origins: [origin], //[origin1, origin2, origin3]

destinations:[destin], //[destinationA, destinationB, destinationC]

travelMode: google.maps.TravelMode.DRIVING,

unitSystem: google.maps.UnitSystem.METRIC,

avoidHighways: false,

avoidTolls: false

JavaScript
              }, callback);
          // }
        }
        function callback(response, status) {
            if (status != google.maps.DistanceMatrixStatus.OK) {
                alert('Error was: ' + status);
            } else {
                var origins = response.originAddresses;
                var destinations = response.destinationAddresses;
                var outputDiv = document.getElementById('outputDiv');
                outputDiv.innerHTML = '';
                deleteOverlays();
                for (var i = 0; i
                    var results = response.rows[i].elements;
                    addMarker(origins[i], false);
                    alert(results[i].duration.text);
                   // for (var j = 0; j
                        addMarker(destinations[i], true); //was j
                        outputDiv.innerHTML += origins[i] + ' to ' + destinations[i] //was destination[j]

+ ': ' + results[i].distance.text + ' in ' //results[j].distance.text

+ results[i].duration.text + '
'; //results[j].distance.text

Code
                    //}

                }
            }
        }

6 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Are you forming origin & destination like below example:

    C#
    var origin1 = new google.maps.LatLng(55.930385, -3.118425);
    var origin2 = "Greenwich, England";
    var destinationA = "Stockholm, Sweden";
    var destinationB = new google.maps.LatLng(50.087692, 14.421150);
    
    
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
      {
        origins: [origin1, origin2],
        destinations: [destinationA, destinationB],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: UnitSystem,
        durationInTraffic: Boolean,
        avoidHighways: false,
        avoidTolls: false
      }, callback);


    can you please paste your values origin1 to origin3 and for destination variables.
    0
  • 0
    Here is how I create the origins and destinations
     var origin1 = new google.maps.LatLng(26.368628, -80.137214);
      var origin2 = new google.maps.LatLng(26.368311, -80.131249);
      var origin3 = new google.maps.LatLng(26.368215, -80.128964);
      var origin4 = new google.maps.LatLng(26.368099, -80.125042);
      var origin5 = new google.maps.LatLng(26.368176, -80.120965);
      var origin6 = new google.maps.LatLng(26.367907, -80.116363);
      var origin7 = new google.maps.LatLng(26.367475, -80.114678);
      var origin8 = new google.maps.LatLng(26.365442, -80.106143);
      var destinationA = new google.maps.LatLng(26.368311, -80.131249);
      var destinationB = new google.maps.LatLng(26.368215, -80.128964);
      var destinationC = new google.maps.LatLng(26.368099, -80.125042);
      var destinationD = new google.maps.LatLng(26.368176, -80.120965);
      var destinationE = new google.maps.LatLng(26.367907, -80.116363);
      var destinationF = new google.maps.LatLng(26.367475, -80.114678);
      var destinationG = new google.maps.LatLng(26.365442, -80.106143);
      var destinationH = new google.maps.LatLng(26.362884, -80.098408);
      var destinationIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=D|FF0000|000000';
      var originIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=O|FFFF00|000000';
      function initialize() {
      var opts = {
      center: new google.maps.LatLng(26.368049, -80.116693),
      zoom: 15
      };
      map = new google.maps.Map(document.getElementById('map-canvas'), opts);
      geocoder = new google.maps.Geocoder();
      }
     
      function calculateDistances() {
     
      var service = new google.maps.DistanceMatrixService();
      service.getDistanceMatrix(
      {
      origins: [origin1, origin2, origin3, origin4, origin5, origin6, origin7, origin8], 
      destinations: [destinationA, destinationB, destinationC, destinationD, destinationE, destinationF, destinationG, destinationH], 
      travelMode: google.maps.TravelMode.DRIVING,
      unitSystem: google.maps.UnitSystem.METRIC,
      avoidHighways: false,
      avoidTolls: false
      }, callback);
     
      }


    But instead of [origin1, origin2, origin3, origin4, origin5, origin6, origin7, origin8],  I thought maybe there is some more intelligent way
    0
  • 0
    the only difference that i can see is how origin and destination pairs are created. 

    var origin1 = new google.maps.LatLng(55.930385, -3.118425);
    var origin2 = "Greenwich, England";
    var destinationA = "Stockholm, Sweden";
    var destinationB = new google.maps.LatLng(50.087692, 14.421150);

    can you try creating your origin & destination pairs like above rather having so many
    0
  • 0


    The first attempt was just yours, the same example from Google developers site But I need dynamically created list I can keep it like this and it works, but I wanted to use an array or similar to solve it in more elegant way

    But thanks for your idea anyway, and for your try to help

    0
  • 0
    you can make it dynamic by passing values like this:

    var origin1 = new google.maps.LatLng(array1[0], array1[1]);

    my belief is that Google API is not accepting more than requirement number of origins & destinations as desired. So limit it upto what Google example suggest and make it dynamic the way I suggested at top. If you have more origin & destination then i suggest to loop it, e.g.,

    _.each(array1, index, function(a){
           var origin1 = new google.maps.LatLng(a, a[index+1]);      
    });

    I constructed above loop from Underscore.js you can use the regular FOR loop of JavaScript

    Please click checkbox "This is correct answer" if you find it helpful!
    0
  • 0


    Hi Sumit,

    But can you please write an example with this using the origins and destinations I posted?

    Thank you very much

    0