wpf-devexpress-dot-xpf-dot-map-c349799d.md
Allows you to communicate with the Azure Maps service to obtain information about traffic incidents and display them on the map.
Namespace : DevExpress.Xpf.Map
Assembly : DevExpress.Xpf.Map.v25.2.dll
NuGet Package : DevExpress.Wpf.Map
public class AzureTrafficIncidentDataProvider :
AzureMapDataProviderBase
Public Class AzureTrafficIncidentDataProvider
Inherits AzureMapDataProviderBase
The AzureTrafficIncidentDataProvider allows you to obtain information about traffic incidents from 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 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:
<Window xmlns:dxm="http://schemas.devexpress.com/winfx/2008/xaml/map"...>
<Window.Resources>
<sys:String x:Key="azureKey">Your AzureMap key here</sys:String>
</Window.Resources>
<Grid>
<dxm:MapControl Loaded="MapControl_Loaded" x:Name="mapControl"
ToolTipEnabled="True" ShowSearchPanel="False" ZoomLevel="3">
<dxm:MapControl.ZoomTrackbarOptions>
<dxm:ZoomTrackbarOptions Visible="False" />
</dxm:MapControl.ZoomTrackbarOptions>
<dxm:ImageLayer>
<dxm:AzureMapDataProvider AzureKey="{StaticResource azureKey}" Tileset="BaseRoad"/>
</dxm:ImageLayer>
<dxm:InformationLayer x:Name="infoLayer">
<dxm:AzureTrafficIncidentDataProvider AzureKey="{StaticResource azureKey}" x:Name="trafficIncidentProvider"
LayerItemsGenerating="OnLayerItemsGenerating"/>
</dxm:InformationLayer>
</dxm:MapControl>
</Grid>
</Window>
using DevExpress.Xpf.Map;
using System.Windows;
namespace WpfMapExample {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void MapControl_Loaded(object sender, RoutedEventArgs e) {
trafficIncidentProvider.RequestTrafficIncidents(
new SearchBoundingBox { WestLongitude = - 115.338457, NorthLatitude = 36.268745, EastLongitude = - 114.988268, SouthLatitude = 36.1010376 },
boundingZoom: 18,
trafficModelId: -1,
new AzureTrafficIncidentOptions {
OriginalPosition = false,
IncidentGeometryType = AzureTrafficIncidentGeometryType.Shifted
});
}
private void OnLayerItemsGenerating(object sender, LayerItemsGeneratingEventArgs args) {
mapControl.ZoomToFit(args.Items);
}
}
}
Imports DevExpress.Xpf.Map
Imports System.Windows
Namespace WpfMapExample
Public Partial Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub MapControl_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
trafficIncidentProvider.RequestTrafficIncidents(New SearchBoundingBox With {
.WestLongitude = -115.338457,
.NorthLatitude = 36.268745,
.EastLongitude = -114.988268,
.SouthLatitude = 36.1010376
}, boundingZoom:=18, trafficModelId:=-1, New AzureTrafficIncidentOptions With {
.OriginalPosition = False,
.IncidentGeometryType = AzureTrafficIncidentGeometryType.Shifted
})
End Sub
Private Sub OnLayerItemsGenerating(ByVal sender As Object, ByVal args As LayerItemsGeneratingEventArgs)
mapControl.ZoomToFit(args.Items)
End Sub
End Class
End Namespace
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 ListBoxEdit:
<Window xmlns:dxm="http://schemas.devexpress.com/winfx/2008/xaml/map"...>
<Window.Resources>
<sys:String x:Key="azureKey">Your AzureMaps key here</sys:String>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<dxe:ListBoxEdit Grid.Column="0" x:Name="listbox"/>
<dxm:MapControl Loaded="MapControl_Loaded" Grid.Column="1" x:Name="mapControl"
ToolTipEnabled="True" ShowSearchPanel="False" ZoomLevel="3">
<dxm:MapControl.ZoomTrackbarOptions>
<dxm:ZoomTrackbarOptions Visible="False" />
</dxm:MapControl.ZoomTrackbarOptions>
<dxm:ImageLayer>
<dxm:AzureMapDataProvider AzureKey="{StaticResource azureKey}" Tileset="BaseRoad"/>
</dxm:ImageLayer>
<dxm:InformationLayer x:Name="infoLayer">
<dxm:AzureTrafficIncidentDataProvider AzureKey="{StaticResource azureKey}" x:Name="trafficIncidentProvider"
LayerItemsGenerating="OnLayerItemsGenerating"
TrafficIncidentCalculated="trafficIncidentProvider_TrafficIncidentCalculated"/>
</dxm:InformationLayer>
</dxm:MapControl>
</Grid>
</Window>
using DevExpress.Xpf.Map;
using System.Text;
using System.Windows;
namespace WpfMapExample {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void MapControl_Loaded(object sender, RoutedEventArgs e) {
trafficIncidentProvider.RequestTrafficIncidents(
new SearchBoundingBox { WestLongitude = - 115.338457, NorthLatitude = 36.268745, EastLongitude = - 114.988268, SouthLatitude = 36.1010376 },
boundingZoom: 18,
trafficModelId: -1,
new AzureTrafficIncidentOptions {
OriginalPosition = false,
IncidentGeometryType = AzureTrafficIncidentGeometryType.Shifted
});
}
private void trafficIncidentProvider_TrafficIncidentCalculated(object sender, AzureTrafficIncidentCalculatedEventArgs e) {
if (e.Cancelled) return;
if (e.RequestResult.ResultCode != RequestResultCode.Success) {
listbox.Items.Add ("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++;
}
listbox.Items.Add(resultList.ToString());
}
private void OnLayerItemsGenerating(object sender, LayerItemsGeneratingEventArgs args) {
mapControl.ZoomToFit(args.Items);
}
}
}
Imports DevExpress.Xpf.Map
Imports System.Text
Imports System.Windows
Namespace WpfMapExample
Public Partial Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub MapControl_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
trafficIncidentProvider.RequestTrafficIncidents(New SearchBoundingBox With {
.WestLongitude = -115.338457,
.NorthLatitude = 36.268745,
.EastLongitude = -114.988268,
.SouthLatitude = 36.1010376
}, boundingZoom:=18, trafficModelId:=-1, New AzureTrafficIncidentOptions With {
.OriginalPosition = False,
.IncidentGeometryType = AzureTrafficIncidentGeometryType.Shifted
})
End Sub
Private Sub trafficIncidentProvider_TrafficIncidentCalculated(ByVal sender As Object, ByVal e As AzureTrafficIncidentCalculatedEventArgs)
If e.Cancelled Then Return
If e.RequestResult.ResultCode <> RequestResultCode.Success Then
listbox.Items.Add("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 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
listbox.Items.Add(resultList.ToString())
End Sub
Private Sub OnLayerItemsGenerating(ByVal sender As Object, ByVal args As LayerItemsGeneratingEventArgs)
mapControl.ZoomToFit(args.Items)
End Sub
End Class
End Namespace
Object DispatcherObject DependencyObject Freezable MapDependencyObject InformationDataProviderBase WebInformationDataProvider<DevExpress.Map.Native.JsonProvidedEventArgs> AzureMapDataProviderBase AzureTrafficIncidentDataProvider
See Also