Skip to content

Your first map enabled desktop application

Hossein Narimani Rad edited this page Sep 28, 2025 · 2 revisions

πŸ—ΊοΈ Your First Map-Enabled Desktop Application

.NET License

Learn how to build a desktop mapping application from scratch using WPF (Windows Presentation Foundation) and the Maptor spatial library. This tutorial is designed for beginners with no prior WPF experience.

Maptor Sample Application Screenshot

🎯 What You'll Build

By the end of this tutorial, you'll have a fully functional desktop application that can:

  • Display interactive maps
  • Add shapefile data to the map
  • Measure distances and areas
  • Navigate around the map

πŸ“‹ Prerequisites

Before starting, make sure you have:

  • .NET 8 SDK installed
  • Visual Studio 2022 (Community edition is free) or Visual Studio Code
  • Basic knowledge of C# (variables, methods, classes)

Note: If you're completely new to C#, we recommend going through a basic C# tutorial first.

πŸš€ Step 1: Create Your First WPF Project

What is WPF?

WPF (Windows Presentation Foundation) is Microsoft's framework for building desktop applications on Windows. It uses XAML (eXtensible Application Markup Language) to design user interfaces and C# for the application logic.

Creating the Project

  1. Open Visual Studio and select "Create a new project"
  2. Search for "WPF" and select "WPF Application" (make sure it targets .NET 8)
  3. Name your project (e.g., "MyFirstMapApp")
  4. Choose a location and click "Create"

Create WPF Project

Understanding the Project Structure

After creating the project, you'll see these files:

  • MainWindow.xaml - The visual design of your main window
  • MainWindow.xaml.cs - The C# code behind the window
  • App.xaml - Application-level settings and resources
  • App.xaml.cs - Application startup code

πŸš€ Step 2: Install Required Packages

Installing the Maptor Package

  1. Right-click on your project in Solution Explorer
  2. Select "Manage NuGet Packages"
  3. Go to the "Browse" tab
  4. Search for "IRI.Maptor.Jab.Controls"
  5. Click "Install"

Alternatively, you can use the Package Manager Console:

dotnet add package IRI.Maptor.Jab.Controls 

πŸš€ Step 3: Configure Application Resources

What are Application Resources?

Application resources in WPF are reusable styles, templates, and other UI elements that can be shared across your entire application. We need to configure these to make the Maptor controls look and work properly.

Setting Up App.xaml

  1. Open App.xaml in your project
  2. Replace the existing content with the following:
<Application x:Class="MyFirstMapApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- MahApps.Metro Styles for Modern UI -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.Slider.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.RadioButton.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.TabControl.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Amber.xaml" />

                <!-- Maptor Custom Styles -->
                <ResourceDictionary Source="pack://application:,,,/IRI.Maptor.Jab.Common;component/Assets/Styles/ButtonStyles.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/IRI.Maptor.Jab.Common;component/Assets/Shapes/Others.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/IRI.Maptor.Jab.Common;component/Assets/IRI.Converters.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/IRI.Maptor.Jab.Common;component/Assets/IRI.Fonts.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/IRI.Maptor.Jab.Common;component/Assets/IRI.Colors.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

πŸš€ Step 4: Design Your Main Window

Understanding XAML

XAML (eXtensible Application Markup Language) is used to design the user interface. It's similar to HTML but for desktop applications.

Setting Up MainWindow.xaml

  1. Open MainWindow.xaml
  2. Replace the existing content with the following:
<Window x:Class="MyFirstMapApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:maptor="clr-namespace:IRI.Maptor.Jab.Controls.View;assembly=IRI.Maptor.Jab.Controls"
        Title="My First Map Application" 
        Height="600" 
        Width="800"
        Loaded="Window_Loaded">
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        
        <!-- Toolbar with buttons using predefined commands -->
        <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10" Background="LightGray">
            <Button Content="Add Shapefile" Command="{Binding AddShapefileCommand}" Margin="5" Padding="10,5"/>
            <Button Content="Measure Length" Command="{Binding MeasureLengthCommand}" Margin="5" Padding="10,5"/>
            <Button Content="Measure Area" Command="{Binding MeasureAreaCommand}" Margin="5" Padding="10,5"/>
        </StackPanel>
        
        <!-- Map Control -->
        <maptor:MapViewer x:Name="map" Grid.Row="1" BorderBrush="Black" BorderThickness="1"/>
        
        <!-- Map Info Panel -->
        <maptor:MapInfoView DataContext="{Binding}" Grid.Row="1" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="10"/>
    </Grid>
</Window>

πŸš€ Step 5: Add the C# Code

Setting Up MainWindow.xaml.cs

  1. Open MainWindow.xaml.cs
  2. Replace the existing content with the following:
using System;
using System.Windows;
using IRI.Maptor.Jab.Controls.Presenter;
using IRI.Maptor.Jab.Common;

namespace MyFirstMapApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                // Initialize encoding support for international characters
                System.Text.Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

                // Create and initialize the map presenter (viewmodel)
                var presenter = new MapApplicationPresenter();
                await this.map.Register(presenter);
                presenter.Initialize(this);

                // Configure the map
                presenter.SelectedMapProvider = TileMapProviderFactory.GoogleRoadMap;
                presenter.ZoomToExtent(BoundingBoxes.WebMercator_Africa, false, isNewExtent: true);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error initializing map: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}

Understanding the Code

  • Window_Loaded - This method runs when the window is first displayed
  • MapApplicationPresenter - This is the "brain" of the map that handles all map operations
  • this.map.Register(presenter) - This connects the map control to the presenter
  • presenter.Initialize(this) - This sets up the presenter with the current window
  • TileMapProviderFactory.GoogleRoadMap - This sets Google Maps as the base layer
  • BoundingBoxes.WebMercator_Africa - This sets the initial view to show Africa
  • No button click handlers needed! - The buttons use predefined commands that work automatically

πŸš€ Step 6: Test Your Application

Running the Application

  1. Press F5 or click the "Start" button in Visual Studio
  2. Your application should start and show a map of Africa
  3. Try the buttons to test the functionality

πŸŽ‰ Congratulations!

You've successfully created your first map-enabled desktop application!

What You've Learned

  • How to create a WPF application
  • How to use XAML to design user interfaces
  • How to integrate third-party controls (Maptor)
  • How to handle user interactions with C#
  • How to display interactive maps in a desktop application

Next Steps

  • Try adding more features to your application
  • Experiment with different map providers
  • Add more buttons and functionality
  • Learn more about WPF layouts and styling

πŸ“‚ Complete Sample Code

For a more advanced example with additional features, check out the complete sample project.

πŸ”— Additional Resources

Maptor Wiki

🏠 Home

πŸ“š Library Groups

πŸ“¦ IRI.Maptor.Sta

πŸ“ IRI.Maptor.Sta.ShapefileFormat

πŸ”§ IRI.Maptor.Ket

Coming Soon

πŸ–₯️ IRI.Maptor.Jab

Coming Soon

πŸš€ Getting Started

πŸ”— External Resources


Maptor Spatial Library Ecosystem

Clone this wiki locally