Back to Devexpress

MbTilesDataProvider Class

windowsforms-devexpress-dot-xtramap-0b77be4b.md

latest5.2 KB
Original Source

MbTilesDataProvider Class

A data provider that loads vector tiles from a MbTiles database.

Namespace : DevExpress.XtraMap

Assembly : DevExpress.XtraMap.v25.2.dll

NuGet Package : DevExpress.Win.Map

Declaration

csharp
public class MbTilesDataProvider :
    VectorTileDataProviderBase
vb
Public Class MbTilesDataProvider
    Inherits VectorTileDataProviderBase

Remarks

A set of tiles can be packaged in MbTiles files (wrapped SQLite databases) stored locally or on a server. MbTiles can also contain raster tiles.

Follow the steps below to load data from a MbTiles file:

  1. Install the System.Data.SQLite.Core package if your application does not reference this library.
  2. Create an image layer and add it to the MapControl.Layers collection.
  3. Create an MbTilesDataProvider instance and assign it to the ImageLayer.DataProvider property.
  4. Use the MbTilesDataProvider.FileUri property to specify a path to an MbTiles file.
csharp
private void Form1_Load(object sender, EventArgs e) {
    ImageLayer layer = new ImageLayer();
    MbTilesDataProvider dataProvider = new MbTilesDataProvider();
    dataProvider.FileUri = new Uri(@"D:\countries.mbtiles", UriKind.Absolute);
    layer.DataProvider = dataProvider;
    mapControl1.Layers.Add(layer);
}
vb
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim layer As ImageLayer = New ImageLayer()
    Dim dataProvider As MbTilesDataProvider = New MbTilesDataProvider()
    dataProvider.FileUri = New Uri("D:\countries.mbtiles", UriKind.Absolute)
    layer.DataProvider = dataProvider
    mapControl1.Layers.Add(layer)
End Sub

Apply a Custom Style

If a default vector tile style does not meet your requirements, you can apply a custom style. Use the VectorTileDataProviderBase.StyleFileUri property to define a path to a style file. The MapControl uses System.Text.Json library to parse style files. Install the System.Text.Json package if your .NET Framework application does not reference this library. .NET projects do not require manual installation of the System.Text.Json package, as it is already included in the .NET environment. Set the DevExpress.Map.Native.VectorTileStyleParser.ProcessingLibrary property to NewtonsoftJson to use the Newtonsoft.Json library instead. See Vector Tile Providers: Vector Tile Styles for more information about styles.

The following code loads a style file from the project’s Data directory:

csharp
dataProvider.StyleFileUri = new Uri(GetRelativePath("style.json"), UriKind.Absolute);

public static string GetRelativePath(string name) {
    name = "Data\\" + 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;
}
vb
dataProvider.StyleFileUri = New Uri(GetRelativePath("style.json"), UriKind.Absolute)

Public Shared Function GetRelativePath(ByVal name As String) As String
    name = "Data\" & name
    Dim dir As DirectoryInfo = New DirectoryInfo(Application.StartupPath)
    While dir IsNot Nothing
        Dim filePath = Path.Combine(dir.FullName, name)
        If File.Exists(filePath) Then Return filePath
        dir = Directory.GetParent(dir.FullName)
    End While
    Return String.Empty
End Function

Inheritance

Object MapDisposableObject MapImageDataProviderBase MapTileDataProviderBase ImageTileDataProvider VectorTileDataProviderBase MbTilesDataProvider

See Also

MbTilesDataProvider Members

DevExpress.XtraMap Namespace