|
1 | | -# Integrate-.NET-MAUI-Maps-with-android-native-embedding |
2 | | -This repository contains a sample that explain how to integrate .NET MAUI SfMaps with android native embedding. |
| 1 | +# How to integrate .NET MAUI Maps with android native embedding application? |
| 2 | +In this article, you will learn how to create a [.NET MAUI Maps](https://www.syncfusion.com/maui-controls/maui-maps) native embedded Android application by following the step by step process explained below. |
| 3 | + |
| 4 | +**Step 1:** |
| 5 | +Create a .NET Android application and install the [Syncfusion.Maui.Maps](https://www.nuget.org/packages/Syncfusion.Maui.Maps) nuget package using the [nuget.org](https://www.nuget.org/). |
| 6 | + |
| 7 | +**Step 2:** |
| 8 | +In the project file of the native application, add the tag `<UseMaui>true</UseMaui>` to enable the .NET MAUI support as demonstrated below. |
| 9 | + |
| 10 | +**[XML]:** |
| 11 | + ```xml |
| 12 | +<PropertyGroup> |
| 13 | + <Nullable>enable</Nullable> |
| 14 | + <ImplicitUsings>enable</ImplicitUsings> |
| 15 | + <UseMaui>true</UseMaui> |
| 16 | +</PropertyGroup> |
| 17 | + ``` |
| 18 | + |
| 19 | +**Step 3:** |
| 20 | +Initialize .NET MAUI in the native app project by creating a **MauiAppBuilder** object and using the **UseMauiEmbedding** function. Then, use the **Build()** method on the **MauiAppBuilder** object to build a **MauiApp** object. Finally, create a **MauiContext** object from the MauiApp object to convert .NET MAUI controls to native types. |
| 21 | + |
| 22 | +**[C#]:** |
| 23 | + ```csharp |
| 24 | +MauiContext? _mauiContext; |
| 25 | +protected override void OnCreate(Bundle? savedInstanceState) |
| 26 | +{ |
| 27 | + base.OnCreate(savedInstanceState); |
| 28 | + MauiAppBuilder builder = MauiApp.CreateBuilder(); |
| 29 | + builder.UseMauiEmbedding<Microsoft.Maui.Controls.Application>(); |
| 30 | + builder.ConfigureSyncfusionCore(); |
| 31 | + MauiApp mauiApp = builder.Build(); |
| 32 | + _mauiContext = new MauiContext(mauiApp.Services, this); |
| 33 | +} |
| 34 | + ``` |
| 35 | + |
| 36 | +**Step 4:** |
| 37 | +Create a new instance for the SfMaps control, add a shape layer to it, and set the source of the map shapes using the [ShapesSource](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Maps.MapShapeLayer.html#Syncfusion_Maui_Maps_MapShapeLayer_ShapesSource) property of the [MapShapeLayer](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.Maps.MapShapeLayer.html). |
| 38 | + |
| 39 | +**[C#]:** |
| 40 | + ```csharp |
| 41 | +protected override void OnCreate(Bundle? savedInstanceState) |
| 42 | +{ |
| 43 | + ... |
| 44 | + SfMaps map = new SfMaps(); |
| 45 | + MapShapeLayer layer = new MapShapeLayer(); |
| 46 | + layer.ShapesSource = MapSource.FromUri(new Uri("https://cdn.syncfusion.com/maps/map-data/world-map.json")); |
| 47 | + map.Layer = layer; |
| 48 | + ... |
| 49 | +} |
| 50 | + ``` |
| 51 | + |
| 52 | +**Step 5:** |
| 53 | + |
| 54 | +Convert the Maps control to a platform-specific view for the .NET MAUI framework and set this view as the content view for the current Android activity. |
| 55 | + |
| 56 | +**[C#]:** |
| 57 | + ```csharp |
| 58 | +protected override void OnCreate(Bundle? savedInstanceState) |
| 59 | +{ |
| 60 | + Android.Views.View view = map.ToPlatform(_mauiContext); |
| 61 | + |
| 62 | + // Set our view from the "main" layout resource |
| 63 | + SetContentView(view); |
| 64 | +} |
| 65 | + ``` |
| 66 | + |
| 67 | +**Output:** |
| 68 | + |
| 69 | +  |
0 commit comments