DistanceMatrix Passing StartTime in JSON RRS feed

  • Question

  • Hello everyone

    I am using DistanceMatrix API to get a distance matrix for 13 coordinates. The coordinates data is being passed using HTTPOST and JSON. The problem I am facing is the if I specify "startTime" in my JSON data, I get a "400-Bad Request" error. If I omit the "startTime" it works fine. I am using JSON.Net to serialize the data that is being posted to BingMaps. The code and JSON is given below. I have tried to specify "startTime" field in MatrixData class as a string, date and DateTimeOffset but nothing works. As far as I can tell, JSON is formatted correctly. Thanks in advance for your help.

    Serliazed JSON

    {"origins":[{...omitted for brevity...}],"destinations":[{"...omitted for brevity...}],"travelMode":"driving","startTime":"2018-11-08T08:00:00-05:00","distanceUnit":"mi"}


        Private Sub cmdRouteMatrix_Click(sender As Object, e As EventArgs) Handles cmdRouteMatrix.Click

            Me.Cursor = Cursors.WaitCursor
            Dim xml As String = ""
            Dim locations As List(Of Location) = GetRouteMatrixData()
            Dim json As String = GetJson(locations)
            Dim routeUrl As String = "*****&output=xml"

            Dim messageBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(json)

            'Dim body As HttpContent = New StringContent(json)

            Dim body As HttpContent = New ByteArrayContent(messageBytes)
            body.Headers.ContentType = New Headers.MediaTypeHeaderValue("application/json")
            body.Headers.ContentLength = json.Length

                Using client As HttpClient = New HttpClient()
                    Dim response As HttpResponseMessage = client.PostAsync(routeUrl, body).Result
                    If response.IsSuccessStatusCode Then
                        Dim responseContent As HttpContent = response.Content
                        xml = responseContent.ReadAsStringAsync().Result
                        txtResult.Text = xml
                        MessageBox.Show("RouteHelper:GetDirections: Unable to get directions." & vbCrLf &
                                    "Url:" & routeUrl & vbCrLf &
                                    "StatusCode:" & response.StatusCode.ToString())
                    End If
                End Using

            Catch ex As Exception
                Me.Cursor = Cursors.Default
                MsgBox(ex.Message & vbCrLf & ex.StackTrace)
                Me.Cursor = Cursors.Default
            End Try

        End Sub

        Private Function GetJson(ByVal locations As List(Of Location)) As String

            Dim data As MatrixData = New MatrixData
            Dim geoPoints As List(Of Geocodes) = New List(Of Geocodes)
            For Each loc As Location In locations
                geoPoints.Add(New Geocodes() With {.latitude = loc.Latitude, .longitude = loc.Longitude})

            data.destinations = geoPoints.ToArray()
   = geoPoints.ToArray()
            data.travelMode = "driving"
            data.startTime = New DateTimeOffset(2018, 11, 8, 8, 0, 0, New TimeSpan(0, -300, 0))  '<= This will cause the error
            data.distanceUnit = "mi"
            Return JsonConvert.SerializeObject(data)

        End Function

    End Class

    Public Class Geocodes

        Public latitude As Single
        Public longitude As Single

    End Class

    Public Class MatrixData

        Public origins() As Geocodes
        Public destinations() As Geocodes
        Public travelMode As String
        Public startTime As DateTimeOffset '<= This is the problem

        ''' <summary>
        ''' Distance unit in "mi" or "km"
        ''' </summary>
        Public distanceUnit As String

    End Class

    Wednesday, November 7, 2018 10:23 PM

All replies

  • You need to parse the date to the correct format. From the docs:

    startTime Optional for Driving, but required if making asynchronous Driving request. Specifies the start or departure time of the matrix to calculate and uses predictive traffic data. This should be a date string in a format that can be parsed by DateTimeOffset.Parse. This option is only supported when the travel mode is set to driving.

    Example: startTime=2017-06-15T8:00:00-07:00
    Thursday, November 8, 2018 6:47 PM
  • Thanks IoTGirl. I did refer to the docs you have mentioned. As far as i can tell my starting time IS formatted as the API is expecting. See the serialized MatrixData object below. i am not sure what am I missing.

    {"origins":[{...omitted for brevity...}],"destinations":[{"...omitted for brevity...}],"travelMode":"driving","startTime":"2018-11-08T08:00:00-05:00","distanceUnit":"mi"}

    MS Example: startTime=2017-06-15T8:00:00-07:00

    My Example: startTime=2018-11-08T08:00:00-05:00

    Monday, November 12, 2018 1:56 PM
  • Anyone? I will appreciate your help.
    Thursday, November 15, 2018 9:08 PM
  • Have you tried to parse your string? 

    This should be a date string in a format that can be parsed by DateTimeOffset.Parse. This option is only supported when the travel mode is set to driving.

    Friday, November 16, 2018 12:10 AM
  • I did and it parses fine. I even tried the exact string from BingMaps example and got same error. Thanks for your help.
    Wednesday, November 28, 2018 6:52 PM
  • Can you try calling with fewer points?  One team member suggest that the user gets 400 - Bad Request because it is a sync with traffic call and at 13 waypoints it breaks the 10 pairs limit on this type of request.
    Thursday, November 29, 2018 9:20 PM
  • I tried with 10 and then with 9 points. Unfortunately, neither of them worked. Here is the JSON that I passed for 9 points.








    Monday, December 3, 2018 6:39 PM
  • That is still too many pairs when doing traffic.  From a team member:

    The errorDetails indicate that 9X9=81 is greater than the max allowed pairs (10) for timewithTraffic

    : When optimize is timeWithTraffic, the product of the count of origins and destinations must be no more than 10.


    If reduce the waypoints to 3 and 3 (3X3=9), the right results will be returned.

    // example below








    Tuesday, December 4, 2018 6:34 AM
  • Thanks for your response.

    I did try with 3x3 points and it worked. However, 9 points is way too limited for our requirement. Is there a way to use predictive traffic with more points? If no, then are there any plans on part of MS to support more points?

    Tuesday, December 4, 2018 3:28 PM