windowsforms-devexpress-dot-xtramap-6ef54d98.md
Communicates with the Azure Maps service to obtain information about traffic incidents and display them on the map.
Namespace : DevExpress.XtraMap
Assembly : DevExpress.XtraMap.v25.2.dll
NuGet Package : DevExpress.Win.Map
public class AzureTrafficIncidentDataProvider :
AzureMapDataProviderBase
Public Class AzureTrafficIncidentDataProvider
Inherits AzureMapDataProviderBase
The DevExpress.XtraMap.AzureTrafficIncidentDataProvider implements the Azure Maps service. Assign such an object to the InformationLayer.DataProvider property to enable the service’s access to traffic incident information, and display incidents on the map. Specify AzureTrafficIncidentDataProvider.AzureKey to access data from Azure Maps. A key is obtained when you create an Azure Maps account.
Azure Maps services support JSON response formats. Install the System.Text.Json package in projects that target .NET Framework to parse the Azure server response and display information on a DevExpress Map control.
Call the provider’s RequestTrafficIncidents method to receive a list of incidents. The AzureRouteOptions parameter allows you to specify traffic incident options.
The following code displays traffic incidents that occurred in the specified area:
using DevExpress.XtraMap;
// ...
const string key = "your key";
AzureTrafficIncidentDataProvider trafficIncidentProvider;
// ...
public Form1() {
InitializeComponent();
trafficIncidentProvider = new AzureTrafficIncidentDataProvider {
AzureKey = key
};
imageLayer2.DataProvider = new AzureMapDataProvider() {
AzureKey = key,
Tileset = AzureTileset.BaseHybridRoad
};
imageLayer1.DataProvider = new AzureMapDataProvider() {
AzureKey = key,
Tileset = AzureTileset.Imagery,
};
informationLayer1.DataProvider = trafficIncidentProvider;
trafficIncidentProvider.RequestTrafficIncidents(new SearchBoundingBox(-115.338457, 36.268745,
-114.988268, 36.1010376), 18, -1,
new AzureTrafficIncidentOptions {
OriginalPosition = false,
IncidentGeometryType = AzureTrafficIncidentGeometryType.Shifted
});
}
Imports DevExpress.XtraMap
' ...
Private Const key As String = "your key"
Private trafficIncidentProvider As AzureTrafficIncidentDataProvider
Public Sub New()
InitializeComponent()
trafficIncidentProvider = New AzureTrafficIncidentDataProvider With {.AzureKey = key}
imageLayer2.DataProvider = New AzureMapDataProvider() With {
.AzureKey = key,
.Tileset = AzureTileset.BaseHybridRoad
}
imageLayer1.DataProvider = New AzureMapDataProvider() With {
.AzureKey = key,
.Tileset = AzureTileset.Imagery
}
informationLayer1.DataProvider = trafficIncidentProvider
trafficIncidentProvider.RequestTrafficIncidents(New SearchBoundingBox(-115.338457, 36.268745, -114.988268, 36.1010376), 18, -1, New AzureTrafficIncidentOptions With {
.OriginalPosition = False,
.IncidentGeometryType = AzureTrafficIncidentGeometryType.Shifted
})
End Sub
After you obtain a list of incidents in the RequestTrafficIncidents method, the provider raises the TrafficIncidentCalculated event. You can handle the event to access the collection of received incidents or add custom logic.
This example obtains a list of incidents in the specified area from the Azure Maps service and displays information about obtained incidents in a MemoEdit control:
using DevExpress.XtraMap;
using System.Text;
// ...
const string key = "your key";
AzureTrafficIncidentDataProvider trafficIncidentProvider;
// ...
public Form1() {
InitializeComponent();
trafficIncidentProvider = new AzureTrafficIncidentDataProvider {
AzureKey = key
};
imageLayer2.DataProvider = new AzureMapDataProvider() {
AzureKey = key,
Tileset = AzureTileset.BaseHybridRoad
};
imageLayer1.DataProvider = new AzureMapDataProvider() {
AzureKey = key,
Tileset = AzureTileset.Imagery,
};
informationLayer1.DataProvider = trafficIncidentProvider;
informationLayer1.DataRequestCompleted += OnDataRequestCompleted;
trafficIncidentProvider.TrafficIncidentCalculated += OnTrafficIncidentCalculated;
trafficIncidentProvider.RequestTrafficIncidents(new SearchBoundingBox(-115.338457, 36.268745,
-114.988268, 36.1010376), 18, -1,
new AzureTrafficIncidentOptions {
OriginalPosition = false,
IncidentGeometryType = AzureTrafficIncidentGeometryType.Shifted
});
}
void OnDataRequestCompleted(object sender, RequestCompletedEventArgs e) {
mapControl1.ZoomToFitLayerItems();
}
private void OnTrafficIncidentCalculated(object sender, AzureTrafficIncidentCalculatedEventArgs 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 (AzureTrafficIncidentResult resultInfo in e.RequestResult.IncidentResults) {
resultList.Append(string.Format("Incident {0}: \r\n", resCounter));
resultList.Append(string.Format("Cause: {0}\r\n", resultInfo.Cause));
resultList.Append(string.Format("Description: {0}\r\n", resultInfo.Description));
resultList.Append(string.Format("Start Time: {0}\r\n", resultInfo.StartDate));
resultList.Append(string.Format("End Time: {0}\r\n", resultInfo.EndDate));
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();
}
Imports Microsoft.VisualBasic
Imports DevExpress.XtraMap
Imports System.Text
' ...
Private Const key As String = "your key"
Private trafficIncidentProvider As AzureTrafficIncidentDataProvider
' ...
Public Sub New()
InitializeComponent()
trafficIncidentProvider = New AzureTrafficIncidentDataProvider With {.AzureKey = key}
imageLayer2.DataProvider = New AzureMapDataProvider() With {
.AzureKey = key,
.Tileset = AzureTileset.BaseHybridRoad
}
imageLayer1.DataProvider = New AzureMapDataProvider() With {
.AzureKey = key,
.Tileset = AzureTileset.Imagery
}
informationLayer1.DataProvider = trafficIncidentProvider
AddHandler informationLayer1.DataRequestCompleted, AddressOf OnDataRequestCompleted
AddHandler trafficIncidentProvider.TrafficIncidentCalculated, AddressOf OnTrafficIncidentCalculated
trafficIncidentProvider.RequestTrafficIncidents(New SearchBoundingBox(-115.338457, 36.268745, -114.988268, 36.1010376), 18, -1, New AzureTrafficIncidentOptions With {
.OriginalPosition = False,
.IncidentGeometryType = AzureTrafficIncidentGeometryType.Shifted
})
End Sub
Private Sub OnDataRequestCompleted(ByVal sender As Object, ByVal e As RequestCompletedEventArgs)
mapControl1.ZoomToFitLayerItems()
End Sub
Private Sub OnTrafficIncidentCalculated(ByVal sender As Object, ByVal e As AzureTrafficIncidentCalculatedEventArgs)
If e.Cancelled Then
Return
End If
If e.RequestResult.ResultCode <> RequestResultCode.Success Then
memoEdit1.Text = "Traffic incidents were not found for this area."
Return
End If
Dim resultList As New StringBuilder("")
Dim resCounter As Integer = 1
For Each resultInfo As AzureTrafficIncidentResult In e.RequestResult.IncidentResults
resultList.Append(String.Format("Incident {0}: " & vbCrLf, resCounter))
resultList.Append(String.Format("Cause: {0}" & vbCrLf, resultInfo.Cause))
resultList.Append(String.Format("Description: {0}" & vbCrLf, resultInfo.Description))
resultList.Append(String.Format("Start Time: {0}" & vbCrLf, resultInfo.StartDate))
resultList.Append(String.Format("End Time: {0}" & vbCrLf, resultInfo.EndDate))
resultList.Append(String.Format("Lat: {0}, Lon: {1}" & vbCrLf, resultInfo.Point.Latitude, resultInfo.Point.Longitude))
resultList.Append(String.Format(" _______________________" & vbCrLf))
resCounter += 1
Next resultInfo
memoEdit1.Text = resultList.ToString()
End Sub
Object InformationDataProviderBase WebInformationDataProvider AzureMapDataProviderBase AzureTrafficIncidentDataProvider
See Also