Back to Devexpress

BingTrafficIncidentDataProvider.TrafficIncidentCalculated Event

windowsforms-devexpress-dot-xtramap-dot-bingtrafficincidentdataprovider.md

latest11.6 KB
Original Source

BingTrafficIncidentDataProvider.TrafficIncidentCalculated Event

Occurs after the provider has received a list of traffic incidents.

Namespace : DevExpress.XtraMap

Assembly : DevExpress.XtraMap.v25.2.dll

NuGet Package : DevExpress.Win.Map

Declaration

csharp
public event BingTrafficIncidentCalculatedEventHandler TrafficIncidentCalculated
vb
Public Event TrafficIncidentCalculated As BingTrafficIncidentCalculatedEventHandler

Event Data

The TrafficIncidentCalculated event's data class is BingTrafficIncidentCalculatedEventArgs. The following properties provide information specific to this event:

PropertyDescription
CancelledGets a value indicating whether an asynchronous operation has been canceled. Inherited from AsyncCompletedEventArgs.
ErrorGets a value indicating which error occurred during an asynchronous operation. Inherited from AsyncCompletedEventArgs.
RequestResultReturns results of a request to a web service for traffic incident information.
UserStateGets the unique identifier for the asynchronous task. Inherited from AsyncCompletedEventArgs.

The event data class exposes the following methods:

MethodDescription
RaiseExceptionIfNecessary()Raises a user-supplied exception if an asynchronous operation failed. Inherited from AsyncCompletedEventArgs.

Example

How to: Obtain and Display a List of Traffic Incidents

This example obtains a list of incidents in the specified area from the Bing Maps service and displays information about obtained incidents in a MemoEdit control.

  • Create an InformationLayer object and add it to the MapControl.Layers collection.

  • Initialize the layer’s DataProvider property with a BingTrafficIncidentDataProvider object.

  • Specify the provider’s BingKey property.

  • Call the provider’s RequestTrafficIncidents method to receive a list of incidents.

  • Handle the BingTrafficIncidentDataProvider.TrafficIncidentCalculated event to access the list of incidents and display incident-related information in a MemoEdit control.

  • C#

  • VB.NET

csharp
using DevExpress.Map;
using DevExpress.XtraMap;
using System;
using System.Text;
using System.Windows.Forms;

namespace TrafficAndIncidents {
    public partial class Form1 : Form {

        private void Form1_Load(object sender, EventArgs e) {
            // Create a background image layer.
            ImageLayer imageLayer = new ImageLayer();
            mapControl1.Layers.Add(imageLayer);
            BingMapDataProvider provider = new BingMapDataProvider();
            imageLayer.DataProvider = provider;
            provider.BingKey = "Insert your Bing Key.";
            provider.Kind = BingMapKind.RoadLight;

            // Create an information layer.
            InformationLayer infoLayer = new InformationLayer();
            mapControl1.Layers.Add(infoLayer);

            // Create a BingTrafficIncidentDataProvider and assign it to the information layer.
            BingTrafficIncidentDataProvider trafficIncidentDataProvider = new BingTrafficIncidentDataProvider();
            infoLayer.DataProvider = trafficIncidentDataProvider;
            trafficIncidentDataProvider.BingKey = "Insert your Bing Key."
            BingTrafficIncidentSeverity incidentSeverity = BingTrafficIncidentSeverity.LowImpact | BingTrafficIncidentSeverity.Minor | BingTrafficIncidentSeverity.Moderate | BingTrafficIncidentSeverity.Serious;
            BingTrafficIncidentType incidentType = BingTrafficIncidentType.Accident | BingTrafficIncidentType.Construction | BingTrafficIncidentType.Miscellaneous
                                                    | BingTrafficIncidentType.Weather;
            // Request a list of incidents in the specified area.
            trafficIncidentDataProvider.RequestTrafficIncidents(new SearchBoundingBox( -115.338457, 36.268745, -114.988268, 36.1010376),
                                                                incidentSeverity,
                                                                incidentType);
            trafficIncidentDataProvider.TrafficIncidentCalculated += OnTrafficIncidentCalculated;
            infoLayer.DataRequestCompleted += OnDataRequestCompleted;
        }

        private void OnDataRequestCompleted(object sender, RequestCompletedEventArgs e) {
            // Call the ZoomToFitLayerItems method to zoom the map so that it displays all the obtained incidents.
            mapControl1.ZoomToFitLayerItems();
        }

        private void OnTrafficIncidentCalculated(object sender, BingTrafficIncidentCalculatedEventArgs e) {
            if (e.Cancelled) return;
            if (e.RequestResult.ResultCode != RequestResultCode.Success) {
                memoEdit1.Text = "Traffic incidents were not found for this area.";
                return;
            }
            StringBuilder resultList = new StringBuilder("");
            int resCounter = 1;
            foreach (BingTrafficIncidentResult resultInfo in e.RequestResult.IncidentResults) {
                resultList.Append(string.Format("Incident {0}: \r\n", resCounter));
                resultList.Append(string.Format("Type: {0}\r\n", resultInfo.Type));
                resultList.Append(string.Format("Description: {0}\r\n", resultInfo.Description));
                resultList.Append(string.Format("Start Time: {0}\r\n", resultInfo.StartTime));
                resultList.Append(string.Format("End Time: {0}\r\n", resultInfo.EndTime));
                resultList.Append(string.Format("Lat: {0}, Lon: {1}\r\n", resultInfo.Point.Latitude, resultInfo.Point.Longitude));
                resultList.Append(string.Format(" ______________________________ \r\n"));
                resCounter++;
            }
            memoEdit1.Text = resultList.ToString();
        }
    }
}
vb
Imports DevExpress.Map
Imports DevExpress.XtraMap
Imports System
Imports System.Text
Imports System.Windows.Forms

Namespace TrafficAndIncidents
    Public Partial Class Form1
        Inherits Form

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
            ' Create a background image layer.
            Dim imageLayer As ImageLayer = New ImageLayer()
            mapControl1.Layers.Add(imageLayer)
            Dim provider As BingMapDataProvider = New BingMapDataProvider()
            imageLayer.DataProvider = provider
            provider.BingKey = "Insert your Bing Key."
            provider.Kind = BingMapKind.RoadLight

            ' Create an information layer.
            Dim infoLayer As InformationLayer = New InformationLayer()
            mapControl1.Layers.Add(infoLayer)

            ' Create a BingTrafficIncidentDataProvider and assign it to the information layer.
            Dim trafficIncidentDataProvider As BingTrafficIncidentDataProvider = New BingTrafficIncidentDataProvider()
            infoLayer.DataProvider = trafficIncidentDataProvider
            trafficIncidentDataProvider.BingKey = "Insert your Bing Key."
            Dim incidentSeverity As BingTrafficIncidentSeverity = BingTrafficIncidentSeverity.LowImpact Or BingTrafficIncidentSeverity.Minor Or BingTrafficIncidentSeverity.Moderate Or BingTrafficIncidentSeverity.Serious
            Dim incidentType As BingTrafficIncidentType = BingTrafficIncidentType.Accident Or BingTrafficIncidentType.Construction Or BingTrafficIncidentType.Miscellaneous Or BingTrafficIncidentType.Weather
            ' Request a list of incidents in the specified area.
            trafficIncidentDataProvider.RequestTrafficIncidents(New SearchBoundingBox(-115.338457, 36.268745, -114.988268, 36.1010376), incidentSeverity, incidentType)
            trafficIncidentDataProvider.TrafficIncidentCalculated += AddressOf OnTrafficIncidentCalculated
            infoLayer.DataRequestCompleted += AddressOf OnDataRequestCompleted
        End Sub

        Private Sub OnDataRequestCompleted(ByVal sender As Object, ByVal e As RequestCompletedEventArgs)
            ' Call the ZoomToFitLayerItems method to zoom the map so that it displays all the obtained incidents.
            mapControl1.ZoomToFitLayerItems()
        End Sub

        Private Sub OnTrafficIncidentCalculated(ByVal sender As Object, ByVal e As BingTrafficIncidentCalculatedEventArgs)
            If e.Cancelled Then Return

            If e.RequestResult.ResultCode IsNot RequestResultCode.Success Then
                memoEdit1.Text = "Traffic incidents were not found for this area."
                Return
            End If

            Dim resultList As StringBuilder = New StringBuilder("")
            Dim resCounter As Integer = 1

            For Each resultInfo As BingTrafficIncidentResult In e.RequestResult.IncidentResults
                resultList.Append(String.Format("Incident {0}: " & vbCrLf, resCounter))
                resultList.Append(String.Format("Type: {0}" & vbCrLf, resultInfo.Type))
                resultList.Append(String.Format("Description: {0}" & vbCrLf, resultInfo.Description))
                resultList.Append(String.Format("Start Time: {0}" & vbCrLf, resultInfo.StartTime))
                resultList.Append(String.Format("End Time: {0}" & vbCrLf, resultInfo.EndTime))
                resultList.Append(String.Format("Lat: {0}, Lon: {1}" & vbCrLf, resultInfo.Point.Latitude, resultInfo.Point.Longitude))
                resultList.Append(String.Format(" ______________________________" & vbCrLf))
                resCounter += 1
            Next

            memoEdit1.Text = resultList.ToString()
        End Sub
    End Class
End Namespace

See Also

BingTrafficIncidentDataProvider Class

BingTrafficIncidentDataProvider Members

DevExpress.XtraMap Namespace