Minimal example for "Xaml MapControl"

I have mentioned the map control named "Xaml.MapControl" by Clemes Fischer on this blog earlier. This is a very good map control for use in WPF applications and some of the main features are:

  • It can get tiles from many sources such as "Open Street Map", "Bing Maps" and custom sorces by using a url format string.
  • It can also have WMS layers on from different sources, and you may also add your own tile sources or images.
  • You can display UI elements on the map. The mapp add Points, Lines, Elipses and Polygons but you may add xaml-elements yourself to be displayed on your map. 
  • XAML and MVVM support.  Using ItemsControls for multiple elements.
Since it is meant to be used in MVVM and XAML it is very flexible. You can provide data for the map by using WPF binding. You can style it with WPF templates and styles. And adjust data by using WPF converters.

I wanted to write this post to show the most simple and basic way to get a map in a WPF application. And this is the way:

Create project and get the package from Nuget

Make a new WPF solution in Visual Studio.

Inside VS; go to the NuGet Package Manager (Tools + Nuget package manager + Manage Packages for this project).

Search for "Xaml.MapControl" and press install.

Show it on the form

In the MainWindow.xaml add the following inside the grid-tag:

        <map:Map x:Name="MainMap" Center="59.9,10.7" ZoomLevel="4">
            <map:MapTileLayer TileSource="https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png" />
        </map:Map>

You should get some squiggly red lines and an error. This is because the reference to the mapControl's not there. Add the following as a new attribute to the "Window":

		xmlns:map="clr-namespace:MapControl;assembly=MapControl.WPF" 

The full XAML form should then look be something like this:

<Window x:Class="XamlMapBoilerplate.MainWindow"       
		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        xmlns:map="clr-namespace:MapControl;assembly=MapControl.WPF"
        Title="MainWindow" Height="450" Width="800">
    
    <Grid>
        <map:Map x:Name="MainMap" Center="59.9,10.7" ZoomLevel="4">
            <map:MapTileLayer TileSource="https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png" />
        </map:Map>
    </Grid>
</Window>

Press F5  and this is what you should see:


Just in case...

In case it doesn't show up allrigt this may be some tips:

The TileSource in the xample might have changed. You may try some other URL you can find here: https://wiki.openstreetmap.org/wiki/Tile_servers .

The tile server may have implemented new security features or data limits. Check the above link.


Comments

Popular posts from this blog

Custom Pushpin in Bing Maps Control for WPF

Bing Maps Control for WPF with Essential Tools

Tip to enable key-events in WPF