windowsforms-devexpress-dot-xtramap-8fe63911.md
A data adapter that loads data from GeoJSON files, and displays it on vector layers.
Namespace : DevExpress.XtraMap
Assembly : DevExpress.XtraMap.v25.2.dll
NuGet Package : DevExpress.Win.Map
public class GeoJsonFileDataAdapter :
FileDataAdapterBase
Public Class GeoJsonFileDataAdapter
Inherits FileDataAdapterBase
The Map Control allows you to display data from GeoJSON files that store geographical objects in the JSON format.
The following table lists supported GeoJSON elements and corresponding map items:
|
GeoJSON element
|
Map item
| | --- | --- | |
Point
|
| |
MultiPoint
|
| |
LineString
|
| |
MultiLineString
|
| |
Polygon, MultiPolygon
|
| |
GeometryCollection
|
|
Follow the steps below to load data from a .GeoJSON file:
private void Form1_Load(object sender, EventArgs e) {
ImageLayer imageLayer = new ImageLayer {
DataProvider = new BingMapDataProvider { BingKey = "Your_BingKey_here", Kind = BingMapKind.Road }
};
mapControl1.Layers.Add(imageLayer);
VectorItemsLayer vectorLayer = new VectorItemsLayer();
mapControl1.Layers.Add(vectorLayer);
GeoJsonFileDataAdapter dataAdapter = new GeoJsonFileDataAdapter();
dataAdapter.FileUri = new Uri(GetRelativePath("Data\\subway-entrances.geojson"), UriKind.RelativeOrAbsolute);
vectorLayer.Data = dataAdapter;
// Use the ItemsLoaded event to customize items the adapter generates.
dataAdapter.ItemsLoaded += OnDataAdapterItemsLoaded;
// You can call the MapControl.ZoomToFitLayerItems method in the DataLoaded event handler
// to zoom the map so that it displays all vector items.
vectorLayer.DataLoaded += OnVectorLayerDataLoaded;
}
private void OnDataAdapterItemsLoaded(object sender, ItemsLoadedEventArgs e) {
foreach (MapDot item in e.Items) {
item.Fill = Color.Red;
item.Size = 8;
}
}
private void OnVectorLayerDataLoaded(object sender, DataLoadedEventArgs e) {
mapControl1.ZoomToFitLayerItems(new List<LayerBase> { (VectorItemsLayer)mapControl1.Layers[1] });
}
public static string GetRelativePath(string name) {
DirectoryInfo dir = new DirectoryInfo(Application.StartupPath);
while (dir != null) {
string filePath = Path.Combine(dir.FullName, name);
if (File.Exists(filePath))
return filePath;
dir = Directory.GetParent(dir.FullName);
}
return string.Empty;
}
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim imageLayer As ImageLayer = New ImageLayer With {
.DataProvider = New BingMapDataProvider With {
.BingKey = "Your_BingKey_here",
.Kind = BingMapKind.Road
}
}
mapControl1.Layers.Add(imageLayer)
Dim vectorLayer As VectorItemsLayer = New VectorItemsLayer()
mapControl1.Layers.Add(vectorLayer)
Dim dataAdapter As GeoJsonFileDataAdapter = New GeoJsonFileDataAdapter()
dataAdapter.FileUri = New Uri(GetRelativePath("Data\subway-entrances.geojson"), UriKind.RelativeOrAbsolute)
vectorLayer.Data = dataAdapter
' Use the ItemsLoaded event to customize items the adapter generates.
dataAdapter.ItemsLoaded += AddressOf OnDataAdapterItemsLoaded
' You can call the MapControl.ZoomToFitLayerItems method in the DataLoaded event handler
' to zoom the map so that it displays all vector items.
vectorLayer.DataLoaded += AddressOf OnVectorLayerDataLoaded
End Sub
Private Sub OnDataAdapterItemsLoaded(ByVal sender As Object, ByVal e As ItemsLoadedEventArgs)
For Each item As MapDot In e.Items
item.Fill = Color.Red
item.Size = 8
Next
End Sub
Private Sub OnVectorLayerDataLoaded(ByVal sender As Object, ByVal e As DataLoadedEventArgs)
mapControl1.ZoomToFitLayerItems(New List(Of LayerBase) From {
CType(mapControl1.Layers(1), VectorItemsLayer)
})
End Sub
Public Shared Function GetRelativePath(ByVal name As String) As String
Dim dir As DirectoryInfo = New DirectoryInfo(Application.StartupPath)
While dir IsNot Nothing
Dim filePath As String = Path.Combine(dir.FullName, name)
If File.Exists(filePath) Then Return filePath
dir = Directory.GetParent(dir.FullName)
End While
Return String.Empty
End Function
Object MapDisposableObject MapDataAdapterBase CoordinateSystemDataAdapterBase DevExpress.XtraMap.OuterDataAdapterBase FileDataAdapterBase GeoJsonFileDataAdapter
See Also