简体   繁体   English

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

[英]How to display (Google) Maps on .Net Maui

I'm playing around with.Net Maui.我在玩.Net Maui。 I'd like to add a map to my demo app.我想在我的演示应用程序中添加一个 map。 Unfortunately it seems that the map control has not been migrated yet.不幸的是,map 控件似乎尚未迁移。 Also it seems that the promised implementation of the control has been removed from the roadmap for RC.此外,承诺的控制实施似乎已从 RC 的路线图中删除。

Also existing projects like this one: https://github.com/amay077/Xamarin.Forms.GoogleMaps doesn't support.Net Maui...还有像这样的现有项目: https://github.com/amay077/Xamarin.Forms.GoogleMaps不支持.Net Maui ...

Does anybody already include a map to a.Net Maui project and could give me some a hint?是否有人已经将 map 添加到 .Net Maui 项目中并可以给我一些提示?

Thx!谢谢!

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.在 MAUI 团队的早期路线图中,它宣布了 2022 年 2 月和 MAUI 版本 12,但与此同时,我们有 3 月底和 MAUI 14,但 map 控制没有进展。 But Xamarin, the predecessor, still have it.但是前身Xamarin还是有的。 Btw.顺便提一句。 that prevents me to move to MAUI.这阻止我搬到毛伊岛。

To use Google or Apple Maps in .NET MAUI, while it is not yet in the Framework, make use of your own handler.要在 .NET MAUI 中使用 Google 或 Apple 地图,虽然它尚未在框架中,但请使用您自己的处理程序。 You can find a detailed blog post on our website .您可以在我们的网站上找到详细的博客文章

But in general, you have to do the following steps:但总的来说,您必须执行以下步骤:

  1. Create a view that represents your map创建一个代表您的 map 的视图
    public class MapView : View, IMapView
    { }

This is the control you use inside your ContentPage.这是您在 ContentPage 中使用的控件。

  1. Create the platform-independent handler-implementation to render your view创建独立于平台的处理程序实现以呈现您的视图

To render your view, MAUI needs a platform-independent entry point.为了呈现您的视图,MAUI 需要一个独立于平台的入口点。

    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这需要在您的 MauiProgram.cs 中注册

.ConfigureMauiHandlers(handlers =>
{
    handlers.AddHandler(typeof(MapHandlerDemo.Maps.Map),typeof(MapHandler));
})
  1. Create the platform-specific handler-implementation创建特定于平台的处理程序实现

A handler tells MAUI how to render your control.处理程序告诉 MAUI 如何呈现您的控件。 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.与 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();
        }
    }

Next step would be to implement your Android handler.下一步是实现您的 Android 处理程序。

I think I'll use this one: https://github.com/Mapsui/Mapsui我想我会用这个: https://github.com/Mapsui/Mapsui

It's an active projects which is running on.Net Maui这是一个在.Net Maui 上运行的活跃项目

Try the GoogleApi package by Michael Vivet on NuGet.在 NuGet 上尝试 Michael Vivet 的 GoogleApi package。 It is compatible with Net5.它与 Net5 兼容。 I have downloaded the source and added Net6 to the dll, it works perfectly.我已经下载了源代码并将 Net6 添加到 dll,它运行良好。

i have ported Xamarin.Forms.GoogleMaps to .NET MAUI.我已将 Xamarin.Forms.GoogleMaps 移植到 .NET MAUI。 Feel free to contact me if u have any issues: https://www.nuget.org/packages/Onion.Maui.GoogleMaps/如果您有任何问题,请随时与我联系: https://www.nuget.org/packages/Onion.Maui.GoogleMaps/

With .net 7.0 there has been added the control Microsoft.Maui.Controls.Maps在 .net 7.0 中添加了控件Microsoft.Maui.Controls.Maps

Documentation: Map文档: Map

Simple demo Maui app: MapDemo简单演示 Maui 应用程序: MapDemo

Try like this please:请尝试这样:

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

Check please also 也请检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM