'How to display (Google) Maps on .Net Maui
I'm playing around with .Net Maui. I'd like to add a map to my demo app. Unfortunately it seems that the map control has not been migrated yet. Also it seems that the promised implementation of the control has been removed from the roadmap for RC.
Also existing projects like this one: https://github.com/amay077/Xamarin.Forms.GoogleMaps doesn't support .Net Maui...
Does anybody already include a map to a .Net Maui project and could give me some a hint?
Thx!
Solution 1:[1]
I also missing it. In an earlier roadmap of the MAUI team it was announced for February/2022 and MAUI Version 12, but meanwhile we have end of March and MAUI 14, but no progress in the map control. But Xamarin, the predecessor, still have it. Btw. that prevents me to move to MAUI.
Solution 2:[2]
Solution 3:[3]
Try the GoogleApi package by Michael Vivet on NuGet. It is compatible with Net5. I have downloaded the source and added Net6 to the dll, it works perfectly.
Solution 4:[4]
To use Google or Apple Maps in .NET MAUI, while it is not yet in the Framework , make use of your own handler. You can find a detailed blog post on our website.
But in general, you have to do the following steps:
- Create a view that represents your map
public class MapView : View, IMapView
{ }
This is the control you use inside your ContentPage.
- Create the platform-independent handler-implementation to render your view
To render your view, MAUI needs a platform-independent entry point.
partial class MapHandler
{
public static IPropertyMapper<MapView, MapHandler> MapMapper = new PropertyMapper<MapView, MapHandler>(ViewMapper)
{ };
public MapHandler() : base(MapMapper)
{ }
}
That needs to be registered in your MauiProgram.cs
.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(MapHandlerDemo.Maps.Map),typeof(MapHandler));
})
- Create the platform-specific handler-implementation
A handler tells MAUI how to render your control. So you need a handler for each platform you want to support. The iOS handler is, in comparison to Android, simpler and shorter to implement.
public partial class MapHandler : ViewHandler<MapView, MKMapView>
{
public MapHandler(IPropertyMapper mapper, CommandMapper commandMapper = null) : base(mapper, commandMapper)
{ }
protected override MKMapView CreatePlatformView()
{
return new MKMapView(CoreGraphics.CGRect.Empty);
}
protected override void ConnectHandler(MKMapView PlatformView)
{ }
protected override void DisconnectHandler(MKMapView PlatformView)
{
// Clean-up the native view to reduce memory leaks and memory usage
if (PlatformView.Delegate != null)
{
PlatformView.Delegate.Dispose();
PlatformView.Delegate = null;
}
PlatformView.RemoveFromSuperview();
}
}
Next step would be to implement your Android handler.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Freudi |
Solution 2 | Amjad S. |
Solution 3 | Paul Mariotti |
Solution 4 | tequila slammer |