Skip to content

Commit

Permalink
Fix for #2050 [DOC] Update Sample Application to demonstrate correct …
Browse files Browse the repository at this point in the history
…way to initialize Maps in MauiProgram.cs to support Maps on all platforms. (#2325)

* Modified sample to support Maps on all platforms.

* Added XML documentation to reflect proper usage of UseCommunityToolkitMaps() in a multi-platform app.
  • Loading branch information
mikelor authored Nov 8, 2024
1 parent 63c0618 commit d42bd2a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 5 deletions.
6 changes: 6 additions & 0 deletions samples/CommunityToolkit.Maui.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ public static MauiApp CreateMauiApp()
.UseMauiCommunityToolkitMarkup()
.UseMauiCommunityToolkitCamera()
.UseMauiCommunityToolkitMediaElement()

#if WINDOWS
.UseMauiCommunityToolkitMaps("KEY") // You should add your own key here from bingmapsportal.com
#else
.UseMauiMaps()
#endif

.UseMauiApp<App>()
.ConfigureFonts(static fonts =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
x:Class="CommunityToolkit.Maui.Sample.Pages.Views.BasicMapsPage"
x:DataType="vm:BasicMapsViewModel"
x:TypeArguments="vm:BasicMapsViewModel"
Title="Windows Map">
Title="Map">

<Grid RowDefinitions="Auto, Auto, Auto, Auto, *">
<HorizontalStackLayout Grid.Row="0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
x:DataType="vm:MapsPinsViewModel"
x:TypeArguments="vm:MapsPinsViewModel"
x:Class="CommunityToolkit.Maui.Sample.Pages.Views.MapsPinsPage"
Title="Windows Maps Pins Sample">
Title="Maps Pins Sample">
<Grid RowDefinitions="Auto, *">
<HorizontalStackLayout Grid.Row="0">
<Button Text="Init region" Clicked="InitRegion_OnClicked" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:enableOnBackInvokedCallback="true"
android:supportsRtl="true">
<service android:name="communityToolkit.maui.media.services" android:stopWithTask="true" android:exported="false" android:enabled="true"

<meta-data android:name="com.google.android.geo.API_KEY" android:value="PASTE-YOUR-API-KEY-HERE" />

<service android:name="communityToolkit.maui.media.services" android:stopWithTask="true" android:exported="false" android:enabled="true"
android:foregroundServiceType="mediaPlayback">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public sealed class ViewsGalleryViewModel() : BaseGalleryViewModel(
SectionModel.Create<CustomSizeAndPositionPopupViewModel>("Custom Size and Positioning Popup", Colors.Red, "Displays a basic popup anywhere on the screen with a custom size using VerticalOptions and HorizontalOptions."),
SectionModel.Create<DrawingViewViewModel>("DrawingView", Colors.Red, "DrawingView provides a canvas for users to \"paint\" on the screen. The drawing can also be captured and displayed as an Image."),
SectionModel.Create<ExpanderViewModel>("Expander Page", Colors.Red, "Expander allows collapse and expand content."),
SectionModel.Create<BasicMapsViewModel>("Windows Maps Basic Page", Colors.Red, "A page demonstrating a basic example of .NET MAUI Maps for Windows."),
SectionModel.Create<BasicMapsViewModel>("Maps Basic Page", Colors.Red, "A page demonstrating a basic example of .NET MAUI Maps"),
SectionModel.Create<LazyViewViewModel>("LazyView", Colors.Red, "LazyView is a view that allows you to load its children in a delayed manner."),
SectionModel.Create<MapsPinsViewModel>("Windows Maps Pins Page", Colors.Red, "A page demonstrating .NET MAUI Maps for Windows with Pins."),
SectionModel.Create<MapsPinsViewModel>("Maps Pins Page", Colors.Red, "A page demonstrating .NET MAUI Maps with Pins."),
SectionModel.Create<MediaElementViewModel>("MediaElement", Colors.Red, "MediaElement is a view for playing video and audio"),
SectionModel.Create<MediaElementCarouselViewViewModel>("MediaElement in CarouselView", Colors.Red, "MediaElement can be used inside a DataTemplate in a CarouselView"),
SectionModel.Create<MediaElementCollectionViewViewModel>("MediaElement in CollectionView", Colors.Red, "MediaElement can be used inside a DataTemplate in a CollectionView"),
Expand Down
25 changes: 25 additions & 0 deletions src/CommunityToolkit.Maui.Maps/AppHostBuilderExtensions.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@ public static class AppHostBuilderExtensions
/// <param name="builder"><see cref="MauiAppBuilder"/> generated by <see cref="MauiApp"/> </param>
/// <param name="key"></param>
/// <returns><see cref="MauiAppBuilder"/></returns>
/// <remarks>
/// In order to use the .NET MAUI Community Toolkit Maps you need to call the extension method in your `MauiProgram.cs` file as follows:
/// </remarks>
/// <example>
/// <code>
/// public static MauiApp CreateMauiApp()
/// {
/// var builder = MauiApp.CreateBuilder();
/// builder
/// .UseMauiApp&amp;lt;App&amp;gt;()
///
/// #if WINDOWS
/// // Initialize the .NET MAUI Community Toolkit Maps by adding the below line of code
/// .UseMauiCommunityToolkitMaps("key")
/// #else
/// .UseMauiMaps()
/// #endif
///
/// // Continue initializing your .NET MAUI App here
///
/// return builder.Build();
/// }
/// </code>
/// </example>
///
public static MauiAppBuilder UseMauiCommunityToolkitMaps(this MauiAppBuilder builder, string key)
{
builder.ConfigureMauiHandlers(handlers =>
Expand Down
7 changes: 7 additions & 0 deletions src/CommunityToolkit.Maui.Maps/ReadMe.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ public static class MauiProgram
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()

#if WINDOWS
// Initialize the .NET MAUI Community Toolkit Maps by adding the below line of code
.UseMauiCommunityToolkitMaps("key")
#else
// For all other platforms
.UseMauiMaps()
#endif

// After initializing the .NET MAUI Community Toolkit, optionally add additional fonts
.ConfigureFonts(fonts =>
{
Expand Down

0 comments on commit d42bd2a

Please sign in to comment.