MindFusion.Wpf Pack Programmer's Guide
Loading Map Files

Maps in the Mapping for WPF are represented by the Map class. Its LoadFromStream method can load map data from files in the ESRI Shapefile format. Shapefiles come with an associated DBF database, that can provide various attribute values for the map elements. To load the DBF database along with the main map file, set the dbfStream parameter of LoadFromFile. The loaded database can be accessed via the Database property, and each record associated with a map element is exposed via the DatabaseRow property of the Shape class.

To display a map once it is loaded, assign it to the BaseMap property of the MapView control, or add a new MapLayer to the view and assign the map to its Map property. The following code demonstrates how to load a map with associated database from the application's XAP file and assign it to BaseMap:

C#  Copy Code

private void OnLoaded(object sender, RoutedEventArgs e)
{
    var mapUri = new Uri("MyApp;component/my_map.shp", UriKind.Relative);
    var dbfUri = new Uri("MyApp;component/my_map.dbf", UriKind.Relative);

    var mapStreamInfo = Application.GetResourceStream(mapUri);
    var dbfStreamInfo = Application.GetResourceStream(dbfUri);

    if (mapStreamInfo != null && dbfStreamInfo != null)
    {
        var map = new Map();
        map.LoadFromStream(mapStreamInfo.Stream, dbfStreamInfo.Stream, "NAME");
        map.AssignColors();

        mapView.BaseMap = map;
    }
}

Visual Basic  Copy Code

Private Sub OnLoaded(sender As Object, e As RoutedEventArgs)

    Dim mapUri = New Uri("MyApp;component/my_map.shp", UriKind.Relative)
    Dim dbfUri = New Uri("MyApp;component/my_map.dbf", UriKind.Relative)

    Dim mapStreamInfo = Application.GetResourceStream(mapUri)
    Dim dbfStreamInfo = Application.GetResourceStream(dbfUri)

    If mapStreamInfo IsNot Nothing AndAlso dbfStreamInfo IsNot Nothing Then
        Dim map = New Map()
        map.LoadFromStream(mapStreamInfo.Stream, dbfStreamInfo.Stream, "NAME")
        map.AssignColors()

        mapView.BaseMap = map
    End If

End Sub

Maps can be loaded as well from XAML code by setting the MapLocation property of a map layer. The following example loads a map file:

Xaml  Copy Code

<ScrollViewer HorizontalScrollBarVisibility="Auto">
    <map:MapView x:Name="mapView">
        <map:MapLayer MapLocation="my_map.shp" DatabaseLocation="my_map.dbf" LabelField="NAME" />
    </map:MapView>
</ScrollViewer>