.NET MAUI - 地图控件加载和 MoveToRegion 方法未按预期工作

问题描述 投票:0回答:1

我正在尝试在页面的一部分中使用 Microsoft.Maui.Controls.Maps 实现 Google Maps 地图,该部分仅在按下按钮时显示。我在移动地图中心时遇到各种问题。我发现,如果在按下按钮时加载地图,则 MoveToRegion(MapSpan) 方法不起作用,并且地图会以坐标点 (0, 0) 为中心加载。为了解决这个问题,我将页面的这一部分隐藏在另一个元素下,并且仅在按下按钮时以实际大小显示它。然而,这并不是最佳选择,因为即使不应该调用 Google Maps API,也会导致使用该服务的成本更高。然而,另一个问题仍然存在。如果我在设置中心后尝试使用 MoveToRegion 方法,它不起作用并且地图不会移动。

有关该项目的更多详细信息:

它基于.NET 7。

Microsoft.Maui.Controls.Maps 版本 7.0.101。

使用MVVM模式。

地图是在带有自定义控件的页面上实现的,在其背后的代码中,当与自定义控件链接的属性发生更改时,将执行 MoveToRegion。

我已经检查过传递的坐标与当前显示为中心的坐标不同,但地图仍然没有移动。

这可能是与在屏幕上加载地图的延迟有关的问题,因为使用了 MVVM 模式。目前这还只是一个假设。

这是 CustomControl 的 XAML:

<maps:Map x:Name="Map"
          IsScrollEnabled="{Binding IsScrollEnabled}"
          IsShowingUser="{Binding IsShowingUser}"
          IsTrafficEnabled="{Binding IsTrafficEnabled}"
          IsZoomEnabled="{Binding IsZoomEnabled}"
          ItemsSource="{Binding Pins}">
    <maps:Map.ItemTemplate>
        <DataTemplate x:DataType="viewModels:MapPinViewModel">
            <maps:Pin Address="{Binding Address}"
                      Label="{Binding Title}"
                      Location="{Binding Location}" />
        </DataTemplate>
    </maps:Map.ItemTemplate>
</maps:Map>

这是自定义控件背后的代码:

using System.Collections.Generic;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Devices.Sensors;
using Microsoft.Maui.Maps;
using xxx.ViewModels;
using static Microsoft.Maui.Controls.BindableProperty;

namespace xxx
{
    public partial class MapView : ContentView
    {
        public static readonly BindableProperty AddressLabelProperty = Create(nameof(AddressLabel), typeof(string), typeof(MapView));
        public static readonly BindableProperty CenterProperty = Create(nameof(Center), typeof(Location), typeof(MapView));
        public static readonly BindableProperty DesiredHeightProperty = Create(nameof(DesiredHeight), typeof(double), typeof(MapView), 350D);
        public static readonly BindableProperty GoToAddressListCommandProperty = Create(nameof(GoToAddressListCommand), typeof(IRelayCommand), typeof(MapView));
        public static readonly BindableProperty IsScrollEnabledProperty = Create(nameof(IsScrollEnabled), typeof(bool), typeof(MapView), false);
        public static readonly BindableProperty IsShowingUserProperty = Create(nameof(IsShowingUserProperty), typeof(bool), typeof(MapView), false);
        public static readonly BindableProperty IsTrafficEnabledProperty = Create(nameof(IsTrafficEnabled), typeof(bool), typeof(MapView), false);
        public static readonly BindableProperty IsZoomEnabledProperty = Create(nameof(IsZoomEnabled), typeof(bool), typeof(MapView), false);
        public static readonly BindableProperty PinsProperty = Create(nameof(Pins), typeof(IEnumerable<MapPinViewModel>), typeof(MapView));
        public new static readonly BindableProperty IsVisibleProperty = Create(nameof(IsVisible), typeof(bool), typeof(MapView), false, propertyChanged: OnIsVisibleChanged);

        public string AddressLabel
        {
            get
            {
                return (string)this.GetValue(AddressLabelProperty);
            }
            set
            {
                this.SetValue(AddressLabelProperty, value);
            }
        }

        public Location Center
        {
            get
            {
                return (Location)this.GetValue(CenterProperty);
            }
            set
            {
                this.SetValue(CenterProperty, value);
            }
        }

        public double DesiredHeight
        {
            get
            {
                return (double)this.GetValue(DesiredHeightProperty);
            }
            set
            {
                this.SetValue(DesiredHeightProperty, value);
            }
        }

        public IRelayCommand GoToAddressListCommand
        {
            get
            {
                return (IRelayCommand)this.GetValue(GoToAddressListCommandProperty);
            }
            set
            {
                this.SetValue(GoToAddressListCommandProperty, value);
            }
        }

        public bool IsScrollEnabled
        {
            get
            {
                return (bool)this.GetValue(IsScrollEnabledProperty);
            }
            set
            {
                this.SetValue(IsScrollEnabledProperty, value);
            }
        }

        public bool IsShowingUser
        {
            get
            {
                return (bool)this.GetValue(IsShowingUserProperty);
            }
            set
            {
                this.SetValue(IsShowingUserProperty, value);
            }
        }

        public bool IsTrafficEnabled
        {
            get
            {
                return (bool)this.GetValue(IsTrafficEnabledProperty);
            }
            set
            {
                this.SetValue(IsTrafficEnabledProperty, value);
            }
        }

        public new bool IsVisible
        {
            get
            {
                return (bool)this.GetValue(IsVisibleProperty);
            }
            set
            {
                this.SetValue(IsVisibleProperty, value);
            }
        }

        public bool IsZoomEnabled
        {
            get
            {
                return (bool)this.GetValue(IsZoomEnabledProperty);
            }
            set
            {
                this.SetValue(IsZoomEnabledProperty, value);
            }
        }

        public IEnumerable<MapPinViewModel> Pins
        {
            get
            {
                return (IEnumerable<MapPinViewModel>)this.GetValue(PinsProperty);
            }
            set
            {
                this.SetValue(PinsProperty, value);
            }
        }

        public MapView()
        {
            this.InitializeComponent();
        }

        private static void OnIsVisibleChanged(BindableObject bindable, object oldValue, object newValue)
        {
            MapView mapView = bindable as MapView;
            if (mapView == null)
                return;

            bool? isVisible = newValue as bool?;
            if (!isVisible.HasValue)
                return;

            if (isVisible.Value)
            {
                mapView.Grid.HeightRequest = mapView.DesiredHeight;

                if (mapView.Center != null)
                {
                    MapSpan mapSpan = MapSpan.FromCenterAndRadius(new Location(mapView.Center.Latitude, mapView.Center.Longitude), new Distance(500));

                    mapView.Map.MoveToRegion(mapSpan);
                }
            }
            else
                mapView.Grid.HeightRequest = 1D;
        }
    }
}
android ios google-maps maui .net-7.0
1个回答
0
投票

我也面临类似的问题,MapSpan 没有将地图移动到指定的坐标。我发现使用时存在冲突问题 Shell.Current.GoToAsync() 用于导航。请参阅以下链接了解更多详细信息: https://github.com/dotnet/maui-samples/issues/343

如果您发现任何解决方法,请在此处更新。

© www.soinside.com 2019 - 2024. All rights reserved.