如何在 .Net Maui 上显示 (Google) 地图

Phi*_*hil 13 .net dictionary maui

我正在玩 .Net Maui。我想将地图添加到我的演示应用程序中。不幸的是,地图控件似乎还没有迁移。此外,承诺的控制实施似乎已从 RC 路线图中删除。

还有像这样的现有项目: https: //github.com/amay077/Xamarin.Forms.GoogleMaps 不支持 .Net Maui...

有人已经包含了 .Net Maui 项目的地图并且可以给我一些提示吗?

谢谢!

teq*_*mer 7

要在 .NET MAUI 中使用 Google 或 Apple 地图(虽然尚未在 Framework 中),请使用您自己的处理程序。您可以在我们的网站上找到详细的博客文章

但一般来说,您必须执行以下步骤:

  1. 创建代表您的地图的视图
    public class MapView : View, IMapView
    { }
Run Code Online (Sandbox Code Playgroud)

这是您在 ContentPage 中使用的控件。

  1. 创建独立于平台的处理程序实现来呈现您的视图

为了渲染视图,MAUI 需要一个独立于平台的入口点。

    partial class MapHandler
    {
        public static IPropertyMapper<MapView, MapHandler> MapMapper = new PropertyMapper<MapView, MapHandler>(ViewMapper)
        { };

        public MapHandler() : base(MapMapper)
        { }
    }
Run Code Online (Sandbox Code Playgroud)

需要在您的 MauiProgram.cs 中注册

.ConfigureMauiHandlers(handlers =>
{
    handlers.AddHandler(typeof(MapHandlerDemo.Maps.Map),typeof(MapHandler));
})
Run Code Online (Sandbox Code Playgroud)
  1. 创建特定于平台的处理程序实现

处理程序告诉 MAUI 如何呈现您的控件。因此,您需要为您想要支持的每个平台提供一个处理程序。与 Android 相比,iOS 处理程序的实现更简单、更短。

    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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

下一步是实现您的 Android 处理程序。

  • @Freudi,teqila slammer 的博客文章错过了 Android 上的一件事:在地图视图上调用 OnResume()。这就是阻止它加载的原因。CallCreateMap 方法应该如下所示: public void CallCreateMap() { _mapView.OnCreate(_bundle); _mapView.OnResume(); _mapView.GetMapAsync(this); 如果您在加载地图时仍然遇到问题,那么我建议您检查 MSDN 指南以使用 Xamarin.GooglePlayServices.Maps nuget:https://learn.microsoft.com/en-us/xamarin/android/platform/maps-and-location /地图/地图-api (2认同)

小智 -2

请尝试这样:

<WebView Source="https://embed.windy.com" />

Run Code Online (Sandbox Code Playgroud)

还请检查

  • 感谢您对 WebView 的建议,但理想的解决方案应该支持用户在地图上放置标记。我也已经了解过 Google Maps API!但我希望找到一个可以在 Android 和 iOS 中使用的解决方案。据我所知,Google Maps API 仅适用于 Android。 (2认同)