A question about distaceMatrix

33 views
Skip to first unread message

Benjamin Smith

unread,
Jul 20, 2011, 4:07:25 AM7/20/11
to google-map...@googlegroups.com
Hi there. I am trying to create a function that calculates the duration of the changed route. I am relatively new to google maps API and I am sure I am making a simple mistake or may be going about it all wrong.
The function is called by
<code>
        google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
        recalcRoute(directionsDisplay.directions);
      });
</code>
<code>
function recalcRoute(result) {
    var waypts_start=[];
    var waypts_end=[];
    var total = 0;
    var ttime = 0;
    var end_point;
    var start_point;
    var myRouteSteps;
    var myroute = result.routes[0];
  for (i = 0; i < myroute.legs.length; i++) {
    total += myroute.legs[i].distance.value;
   
    myRouteSteps = result.routes[0].legs[i];
       
      for (var j = 0; j < myRouteSteps.steps.length; j++) {
              start_point=myRouteSteps.steps[j].start_point;
                start_point =String(start_point);

                start_point=start_point.replace('(', '');
                start_point=start_point.replace(')', '');
                temp = start_point.split(',');
                var start_lat =parseFloat(temp[0]);
                var start_long =parseFloat(temp[1]);
               
            start_point = new google.maps.LatLng(start_lat,start_long);
               
            end_point=myRouteSteps.steps[j].end_point;
                end_point =String(end_point);
                end_point=end_point.replace('(', '');
                end_point=end_point.replace(')', '');
                temp = end_point.split(',');
                var end_lat =parseFloat(temp[0]);
                var end_long =parseFloat(temp[1]);
               
            end_point = new google.maps.LatLng(end_lat,end_long);
           
          waypts_start.push(start_point);
          waypts_end.push(end_point);
        }
  }

  service.getDistanceMatrix(
  {
    origins: [waypts_start],
    destinations: [waypts_end],
    travelMode: google.maps.TravelMode.DRIVING,
    avoidHighways: false,
    avoidTolls: false
  }, callback);
}
</code>

The script does not get to the callback and I receive this error
Error in property <origins>: (Invalid value at position 0: (-28.85575000000001, 153.04764999999998),(-28.85749, 153.05716000000007),(-28.85474, 153.06182),(-28.83969, 153.07271000000003),(-28.78573, 153.12441),(-28.79623, 153.19927000000007),(-28.77588, 153.18682))

Could anyone give me some tips

Benjamin Smith

unread,
Jul 20, 2011, 9:21:07 AM7/20/11
to google-map...@googlegroups.com
I am sure this should work except the waypoints array is obviously in the wrong format. I think it must be I need to use JSON. Can someone confirm this please and/or direct me to where this info might be found.

Enoch Lau

unread,
Jul 20, 2011, 10:19:27 AM7/20/11
to google-map...@googlegroups.com
The 'origins' property of the request is an array of LatLngs. You've passed in an array of array of LatLngs.

Enoch
Reply all
Reply to author
Forward
0 new messages