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.