none
Using Microsoft Traffic API in Visual Basic 2015 RRS feed

  • Question

  • Hello,

    I would like to get traffic information via Microsoft Bing's Traffic API. What code would be the best way to get the information and display it in labels?

    Monday, June 18, 2018 12:55 AM

All replies

  • Hello,

    For starters, this is a REST service, the following lays out the API basics.

    Using the REST Services with .NET provides rudimentary code in a WPF app but can work in Desktop. And this article has tips and tricks.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, June 18, 2018 1:47 AM
    Moderator
  • One needs Bing key and then you need the extraction of the xml or json. I show the raw traffic string in the text box. There is also a map example.


    Imports System.IO
    
    Public Class Form3
    
        Private BingMapsKey As String
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Text = "Bing Maps Api"
    
            BingMapsKey = "enter your api key here"
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            'Point with labels
            'Dim url As String = "http://dev.virtualearth.net/REST/v1/Imagery/Map/AerialWithLabels/35.1377868652344,-106.642822265625/8?mapSize=1000,1000&key=" & BingMapsKey
    
            'rect map
            Dim url As String = "http://dev.virtualearth.net/REST/v1/Imagery/Map/AerialWithLabels?mapArea=33,-108,36,-106&mapSize=600,600&key=" & BingMapsKey
            Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(url)
    
            Using request As System.Net.WebResponse = req.GetResponse
                Using stream As System.IO.Stream = request.GetResponseStream
                    Dim bmp As Bitmap
                    bmp = New Bitmap(System.Drawing.Image.FromStream(stream))
                    PictureBox1.BackgroundImage = CType(bmp.Clone, Bitmap)
                    bmp.Dispose()
                End Using
            End Using
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
            'traffic
            'https://www.programmableweb.com/api/bing-traffic
            'https://msdn.microsoft.com/en-us/library/hh441726
    
            Dim url As String = "http://dev.virtualearth.net/REST/v1/Traffic/Incidents/37,-105,45,-94?key=" & BingMapsKey
            Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(url)
    
            Using request As System.Net.WebResponse = req.GetResponse
                Using stream As System.IO.Stream = request.GetResponseStream
                    Dim sr As StreamReader = New StreamReader(stream)
                    TextBox1.Text = sr.ReadToEnd
                End Using
            End Using
    
        End Sub
    End Class


    Monday, June 18, 2018 1:57 AM
  • Hello Tommytwotrain,

    I really like your example. How do I get the raw data to display the information I need?

    I would just like the description and detour per event to be shown in a label. I would need each event to have it's own label with the description and detour. I would only need to see the most recent event.

    Monday, June 18, 2018 2:44 AM
  • Hello Tommytwotrain,

    I really like your example. How do I get the raw data to display the information I need?

    I would just like the description and detour per event to be shown in a label. I would need each event to have it's own label with the description and detour. I would only need to see the most recent event.

    Austin,

    Yeah well you will have to dig into the bing api docs and create what you want. I just happened to have an example. 

    It is json or XML are you familiar with it? You have to loop the data file and get the data variables you want to read. Its structured to loop the data easily by key words.

    If you put the string in notepad you can format it a bit and see the first paragraph I formated:

    Looks like point is the keyword, then data, cords are in there:


    "resourceSets":[{"estimatedTotal":156,
    "point":
    {"type":"Point","coordinates":[39.14294,-94.675328]},"description":"Closed at 55th Ter - Closed. Construction work.","end":"\
    /Date(1529406000000)\/","incidentId":4569064806197494398,"lastModified":"\
    /Date(1529038765576)\/","roadClosed":true,"severity":4,"source":9,"start":"\
    /Date(1528280700000)\/",
    "toPoint":
    {"type":"Point","coordinates":[39.14301,-94.706897]},"type":5,"verified":true},{"__type":"TrafficIncident:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1","point":{"type":"Point","coordinates":[39.09668,-94.65318]},"description":"Ramp  closed to US-69\/18th St Expy\/Exit 420 - Closed. Construction work.",
    "end":


    That is how this stuff works. Then you loop the data and extract what you want.

    You will have to look all that up at the bing site. I just happened to have done a bit.

    Karen and others are better at the XML etc. I am not even sure what the example returns right now is it json? But could be XML.

    So one needs to learn to do that. I don't think anyone has it done already unless you find example and also better bing api form too???

    :)

    Monday, June 18, 2018 3:16 AM
  • PS Oh here is a json example format from the docs here.

    See how there must be 131 somethings, then resources: type: Point: Coords.... etc start, end. Then you just loop that and shove it into a list or something.

    So this is like one traffic accident, there would be 131? listed like this one maybe.

    The example output had a constructions project point and pointto etc.

    I think there are classes etc to do it easily but I don't know it well. Maybe Karen or someone will show it. Simple search for examples. I think XML is easier?

    "resourceSets":[
          {
             "estimatedTotal":131,
             "resources":[
                {
                   "__type":"TrafficIncident:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
                   "point":{
                      "type":"Point",
                      "coordinates":[
                         38.85135,
                         -94.34033
                      ]
                   },
                   "congestion":"",
                   "description":"MO-150 is closed between 5th Ave S and Court Dr - construction",
                   "detour":"",
                   "end":"\/Date(1310396400000)\/",
                   "incidentId":210546697,
                   "lane":"",
                   "lastModified":"\/Date(1309391096593)\/",
                   "roadClosed":true,
                   "severity":3,
                   "start":"\/Date(1307365200000)\/",
                   "type":9,
                   "verified":true
                },
                {
                   "__type":"TrafficIncident:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
                   "point":{
                      "type":"Point",
                      "coordinates":[
                         38.85872,
                         -94.54638
                      ]
                   },
                   "congestion":"",
                   "description":"Botts Rd is closed between Andrews Rd and 142nd St - construction",
                   "detour":"To go north take US-71 NB to 140th St and go west on 140th St to access Botts Rd- To go south continue west on MO-150 to Thunderbird Rd to 149th St",
                   "end":"\/Date(1315244760000)\/",
                   "incidentId":191097424,
                   "lane":"",
                   "lastModified":"\/Date(1309391096593)\/",
                   "roadClosed":true,
                   "severity":1,
                   "start":"\/Date(1295704800000)\/",
                   "type":9,
                   "verified":true
                }
             ]
          }
       ],
       "statusCode":200,
       "statusDescription":"OK",
       "traceId":"38491198bf6a42f5b7e60c18aa08ec02"
    }


    Monday, June 18, 2018 3:58 AM