- Create a new .NET MAUI application in Visual Studio.
- Syncfusion .NET MAUI components are available in nuget.org. To add SfMaps to your project, open the NuGet package manager in Visual Studio, search for Syncfusion.Maui.Maps and then install it.
- To initialize the control, import the Maps namespace.
- Initialize SfMaps with MapShapeLayer.
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:map="clr-namespace:Syncfusion.Maui.Maps;assembly=Syncfusion.Maui.Maps"
x:Class="GettingStarted_MAUI_Maps.MainPage">
<map:SfMaps>
<map:SfMaps.Layer>
<map:MapShapeLayer/>
</map:SfMaps.Layer>
</map:SfMaps>
</ContentPage>using Syncfusion.Maui.Maps;
namespace GettingStarted_MAUI_Maps;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
} Syncfusion.Maui.Core nuget is a dependent package for all Syncfusion controls of .NET MAUI. In the MauiProgram.cs file, register the handler for Syncfusion core.
using Syncfusion.Maui.Core.Hosting;
namespace GettingStarted_MAUI_Maps;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureSyncfusionCore()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
return builder.Build();
}
}Now, let us define a simple data model that represents geographical data for the map.
public class Model
{
public string State { get; set; }
public string StateCode { get; set; }
public int ID { get; set; }
public Model(string state, string stateCode, int id)
{
State = state;
StateCode = stateCode;
ID = id;
}
}Next, create a ViewModel class and initialize an ObservableCollection of Model objects with Australian states as follows.
using System.Collections.ObjectModel;
public class ViewModel
{
public ObservableCollection<Model> Data { get; set; }
public ViewModel()
{
Data = new ObservableCollection<Model>();
Data.Add(new Model("New South Wales", "New\nSouth Wales", 1));
Data.Add(new Model("Queensland", "Queensland", 2));
Data.Add(new Model("Northern Territory", "Northern\nTerritory", 3));
Data.Add(new Model("Victoria", "Victoria", 4));
Data.Add(new Model("Tasmania", "Tasmania", 5));
Data.Add(new Model("Western Australia", "Western Australia", 6));
Data.Add(new Model("South Australia", "South Australia", 7));
}
}-
Create a
ViewModelinstance and set it as the Grid'sBindingContext. This enables property binding fromViewModelclass. -
Add namespace of
ViewModelclass to your XAML Page, and setBindingContextin XAML.
<Grid xmlns:local="clr-namespace:GettingStarted_MAUI_Maps">
<Grid.BindingContext>
<local:ViewModel/>
</Grid.BindingContext>
</Grid>Adding a MapShapeLayer to the maps and setting the ShapesSource property to load GeoJSON data to create a geographical map view.
-
The maps has Layer as its default content.
-
The ShapesSource can be set from various sources like local file, Uri, or embedded resource.
-
The ShapeDataField property identifies the field in the GeoJSON that contains the shape names.
-
The DataSource property binds the data from ViewModel.
-
The PrimaryValuePath property maps the data to shapes based on the State field.
<map:SfMaps>
<map:SfMaps.Layer>
<map:MapShapeLayer x:Name="layer"
ShapeDataField="STATE_NAME"
DataSource="{Binding Data}"
PrimaryValuePath="State"
ShapeColorValuePath="ID"/>
</map:SfMaps.Layer>
</map:SfMaps>public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
this.layer.ShapesSource = MapSource.FromResource("GettingStarted_MAUI_Maps.australia.json");
}
}The ShowDataLabels property enables data labels to display information about each shape. The DataLabelSettings property customizes the label display using DataLabelPath.
<map:MapShapeLayer ShowDataLabels="True">
<map:MapShapeLayer.DataLabelSettings>
<map:MapDataLabelSettings DataLabelPath="StateCode" />
</map:MapShapeLayer.DataLabelSettings>
</map:MapShapeLayer>The ColorMappings property is used to apply different colors to map shapes based on values. The EqualColorMapping maps specific color values to corresponding data.
<map:MapShapeLayer.ColorMappings>
<map:EqualColorMapping Color="#d0b800"
Value="1"
Text="NSW" />
<map:EqualColorMapping Color="#00d5cf"
Value="2"
Text="Queensland" />
<map:EqualColorMapping Color="#cf4eee"
Value="3"
Text="Victoria" />
<map:EqualColorMapping Color="#4f93d8"
Value="4"
Text="Tasmania" />
</map:MapShapeLayer.ColorMappings>The MapMarker can be used to denote a specific location on the map with a custom symbol.
<map:MapShapeLayer.Markers>
<map:MapMarkerCollection>
<map:MapMarker x:Name="Adelaide"
Latitude="-34.928497"
Longitude="138.600739" />
</map:MapMarkerCollection>
</map:MapShapeLayer.Markers>Tooltips are used to show information about a shape when hovering over it. Enable tooltip by setting the ShowShapeTooltip property of MapShapeLayer.
<map:MapShapeLayer ShowShapeTooltip="True"/>The legend provides information about the map elements and their corresponding colors. Enable it by adding the Legend to the MapShapeLayer. Set SourceType to Shape and Placement to position it.
<map:MapShapeLayer.Legend>
<map:MapLegend SourceType="Shape"
Placement="Bottom" />
</map:MapShapeLayer.Legend>The following code example gives you the complete code of above configurations.
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:map="clr-namespace:Syncfusion.Maui.Maps;assembly=Syncfusion.Maui.Maps"
xmlns:local="clr-namespace:GettingStarted_MAUI_Maps"
x:Class="GettingStarted_MAUI_Maps.MainPage">
<Grid>
<Grid.BindingContext>
<local:ViewModel />
</Grid.BindingContext>
<map:SfMaps>
<map:SfMaps.Layer>
<map:MapShapeLayer x:Name="layer"
ShapeDataField="STATE_NAME"
DataSource="{Binding Data}"
PrimaryValuePath="State"
ShowDataLabels="True"
ShowShapeTooltip="True"
ShapeColorValuePath="ID">
<!--Set Data Label-->
<map:MapShapeLayer.DataLabelSettings>
<map:MapDataLabelSettings DataLabelPath="StateCode" />
</map:MapShapeLayer.DataLabelSettings>
<!--Set Color mapping-->
<map:MapShapeLayer.ColorMappings>
<map:EqualColorMapping Color="#d0b800"
Value="1"
Text="NSW" />
<map:EqualColorMapping Color="#00d5cf"
Value="2"
Text="Queensland" />
<map:EqualColorMapping Color="#cf4eee"
Value="3"
Text="Victoria" />
<map:EqualColorMapping Color="#4f93d8"
Value="4"
Text="Tasmania" />
<map:EqualColorMapping Color="#8b6adf"
Value="5"
Text="WA" />
<map:EqualColorMapping Color="#7bff67"
Value="6"
Text="SA" />
<map:EqualColorMapping Color="#ff4e42"
Value="7"
Text="NT" />
</map:MapShapeLayer.ColorMappings>
<!--Set Markers-->
<map:MapShapeLayer.Markers>
<map:MapMarkerCollection>
<map:MapMarker x:Name="Adelaide"
Latitude="-34.928497"
Longitude="138.600739" />
</map:MapMarkerCollection>
</map:MapShapeLayer.Markers>
<!--Set Legend-->
<map:MapShapeLayer.Legend>
<map:MapLegend SourceType="Shape"
Placement="Bottom" />
</map:MapShapeLayer.Legend>
</map:MapShapeLayer>
</map:SfMaps.Layer>
</map:SfMaps>
</Grid>
</ContentPage>using Syncfusion.Maui.Maps;
namespace GettingStarted_MAUI_Maps;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
this.layer.ShapesSource = MapSource.FromResource("GettingStarted_MAUI_Maps.australia.json");
}
}using System.Collections.ObjectModel;
namespace GettingStarted_MAUI_Maps
{
public class ViewModel
{
public ObservableCollection<Model> Data { get; set; }
public ViewModel()
{
Data = new ObservableCollection<Model>();
Data.Add(new Model("New South Wales", "New\nSouth Wales", 1));
Data.Add(new Model("Queensland", "Queensland", 2));
Data.Add(new Model("Northern Territory", "Northern\nTerritory", 3));
Data.Add(new Model("Victoria", "Victoria", 4));
Data.Add(new Model("Tasmania", "Tasmania", 5));
Data.Add(new Model("Western Australia", "Western Australia", 6));
Data.Add(new Model("South Australia", "South Australia", 7));
}
public class Model
{
public string State { get; set; }
public string StateCode { get; set; }
public int ID { get; set; }
public Model(string state, string stateCode, int id)
{
State = state;
StateCode = stateCode;
ID = id;
}
}
}
}