.NET MAUI Map控件加载及MoveToRegion方法异常求助
问题概述
- 点击按钮才显示的地图区域,使用
Microsoft.Maui.Controls.Maps实现谷歌地图时,调用MoveToRegion(MapSpan)方法失效,地图默认以(0, 0)为中心显示 - 临时将区域隐藏、点击时再设为实际大小的方案,会触发不必要的Google Maps API调用,增加服务成本
- 已设置过中心后再次调用
MoveToRegion,地图仍无法移动
环境信息
- .NET 7
- Microsoft.Maui.Controls.Maps 7.0.101
- 采用MVVM模式,地图通过自定义控件实现,关联属性变更时执行
MoveToRegion操作,已确认传入坐标与当前显示中心不同,推测问题与MVVM模式下地图加载延迟有关
自定义控件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; } } }
内容的提问来源于stack exchange,提问作者Gabriele Zanatta
相关产品推荐
相关产品推荐

