![]() |
VOOZH | about |
Syncfusion AI Assistant
8 Jun 202615 minutes to read
This section explains the steps required to add the maps control with the shape layer and its elements such as data labels, tooltip, markers, and legends. This section covers only basic features needed to know to get started with Syncfusion® maps. Follow the steps below to add .NET MAUI Maps control to your project.
To get start quickly with our .NET MAUI Maps, you can check the below video.
Before proceeding, ensure the following are set up:
The Syncfusion.Maui.Core NuGet is a dependent package for all Syncfusion® .NET MAUI controls. In the MauiProgram.cs file, register the handler for Syncfusion® core.
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Compatibility;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Controls.Xaml;
using Syncfusion.Maui.Core.Hosting;
namespace MyProject
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureSyncfusionCore()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
return builder.Build();
}
}
}Syncfusion.Maui.Maps namespace into your code.xmlns:map="clr-namespace:Syncfusion.Maui.Maps;assembly=Syncfusion.Maui.Maps"using Syncfusion.Maui.Maps;Create an instance for the maps control, and add it as content.
<map:SfMaps>
</map:SfMaps>SfMaps map = new SfMaps();
this.Content = map;Before proceeding, ensure the following are set up:
Ctrl+Shift+P and type .NET:New Project and enter.dotnet add package Syncfusion.Maui.Maps to install the Syncfusion® .NET MAUI Maps NuGet package.dotnet restore.The Syncfusion.Maui.Core NuGet is a dependent package for all Syncfusion® .NET MAUI controls. In the MauiProgram.cs file, register the handler for Syncfusion® core.
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Compatibility;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Controls.Xaml;
using Syncfusion.Maui.Core.Hosting;
namespace MyProject
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureSyncfusionCore()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
return builder.Build();
}
}
}Syncfusion.Maui.Maps namespace into your code.xmlns:map="clr-namespace:Syncfusion.Maui.Maps;assembly=Syncfusion.Maui.Maps"using Syncfusion.Maui.Maps;Create an instance for the maps control, and add it as content.
<map:SfMaps>
</map:SfMaps>SfMaps map = new SfMaps();
this.Content = map;Before proceeding, ensure the following are set up:
dotnet restore
The Syncfusion.Maui.Core NuGet is a dependent package for all Syncfusion® .NET MAUI controls. In the MauiProgram.cs file, register the handler for Syncfusion® core.
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Compatibility;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Controls.Xaml;
using Syncfusion.Maui.Core.Hosting;
namespace MyProject
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureSyncfusionCore()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
return builder.Build();
}
}
}Syncfusion.Maui.Maps namespace into your code.xmlns:map="clr-namespace:Syncfusion.Maui.Maps;assembly=Syncfusion.Maui.Maps"using Syncfusion.Maui.Maps;Create an instance for the maps control, and add it as content.
<map:SfMaps>
</map:SfMaps>SfMaps map = new SfMaps();
this.Content = map;The Layer in SfMaps holds MapShapeLayer. The actual geographical rendering is done in the each MapShapeLayer. The ShapesSource property of the MapShapeLayer is of type MapSource. The ShapesSource can be set as the .json source or shapefile.
IMPORTANT
The Mercator projection is the default projection in the maps.
The ShapesSource property is used to load shapes from different sources:
FromFile returns a MapSource that reads a shape source from a local file.FromUri returns an MapSource that downloads and reads a shape source from a specified URI.FromResource returns a MapSource that reads a shape source file embedded in an assembly.FromStream returns a MapSource that reads a shape source from a stream that supplies source data.SfMaps provides support to load the json data or shapefile from local path.
public MainPage()
{
InitializeComponent();
SfMaps map = new SfMaps();
MapShapeLayer layer = new MapShapeLayer();
layer.ShapesSource = MapSource.FromFile(@"D:\MyProject\usa_state.shp");
map.Layer = layer;
this.Content = map;
}The MapSource.FromFile method requires a string argument, and returns a new MapSource object that reads the data from the shape source file. There’s also an implicit conversion operator that enables the filename to be specified as a string argument to the ShapesSource property
<map:SfMaps>
<map:SfMaps.Layer>
<map:MapShapeLayer ShapesSource="D:\MyProject\usa_state.shp" />
</map:SfMaps.Layer>
</map:SfMaps>public MainPage()
{
InitializeComponent();
SfMaps map = new SfMaps();
MapShapeLayer layer = new MapShapeLayer();
layer.ShapesSource = MapSource.FromFile(@"D:\MyProject\usa_state.shp");
map.Layer = layer;
this.Content = map;
}SfMaps provides support to load the json data or shapefile from the uri.
public MainPage()
{
InitializeComponent();
SfMaps map = new SfMaps();
MapShapeLayer layer = new MapShapeLayer();
layer.ShapesSource = MapSource.FromUri(new Uri("https://cdn.syncfusion.com/maps/map-data/world-map.json"));
map.Layer = layer;
this.Content = map;
}The MapSource.FromUri method requires a Uri argument, and returns a new MapSource object that reads the shape source from the Uri. There’s also an implicit conversion for string-based URIs.
<map:SfMaps>
<map:SfMaps.Layer>
<map:MapShapeLayer ShapesSource="https://cdn.syncfusion.com/maps/map-data/world-map.json" />
</map:SfMaps.Layer>
</map:SfMaps>public MainPage()
{
InitializeComponent();
SfMaps map = new SfMaps();
MapShapeLayer layer = new MapShapeLayer();
layer.ShapesSource = MapSource.FromUri(new Uri("https://cdn.syncfusion.com/maps/map-data/world-map.json"));
map.Layer = layer;
this.Content = map;
}australia.json in the root folder of a project named MyProject will result in a resource ID of MyProject.australia.json. Similarly, placing world1.shp in the Assets folder of a project named MyProject will result in a resource ID of MyProject.Assets.world1.shp
EmbeddedResource option under BuildAction of respective shapefile.NOTE
You can get the
australia.jsonfile here.
public MainPage()
{
InitializeComponent();
SfMaps map = new SfMaps();
MapShapeLayer layer = new MapShapeLayer();
layer.ShapesSource = MapSource.FromResource("MyProject.australia.json");
map.Layer = layer;
this.Content = map;
}SfMaps provides support to load the json data or shapefile as bytes from stream.
public MainPage()
{
InitializeComponent();
SfMaps map = new SfMaps();
MapShapeLayer layer = new MapShapeLayer();
Assembly assembly = Application.Current?.GetType().GetTypeInfo().Assembly;
var jsonStream = assembly?.GetManifestResourceStream("MyProject.Assets.australia.json");
layer.ShapesSource = MapSource.FromStream(jsonStream);
map.Layer = layer;
this.Content = map;
}The following screenshot illustrates the result of the above code.
NOTE
- Get the complete getting started sample from GitHub link.
- You can refer to our .NET MAUI Maps feature tour page for its groundbreaking feature representations. You can also explore our .NET MAUI Maps Example that shows you how to render the Maps in .NET MAUI.