none
DistanceMatrix Passing StartTime in JSON

    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"}

    *********Code***********

        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 = "https://dev.virtualearth.net/REST/v1/Routes/DistanceMatrix?key=*****&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

            Try
                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
                    Else
                        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)
            Finally
                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})
            Next

            data.destinations = geoPoints.ToArray()
            data.origins = 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
    https://msdn.microsoft.com/en-us/library/mt827298.aspx
    Thursday, November 8, 2018 6:47 PM
    Owner
  • 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