英文:
MAUI: How to move the map to the first item of ObservableCollection
问题
我有这个XAML页面:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:vm="clr-namespace:TimeApp.ViewModels"
x:Class="TimeApp.Views.UbicacionPage"
xmlns:maps="clr-namespace:Microsoft.Maui.Controls.Maps;assembly=Microsoft.Maui.Controls.Maps"
xmlns:sensors="clr-namespace:Microsoft.Maui.Devices.Sensors;assembly=Microsoft.Maui.Essentials"
xmlns:model="clr-namespace:TimeApp.Models"
x:DataType="vm:UbicacionViewModel">
<maps:Map x:Name="TheMap" ItemsSource="{Binding Locations}">
<x:Arguments>
<MapSpan>
<x:Arguments>
<sensors:Location Latitude="-33" Longitude="-70">
</sensors:Location>
<x:Double>0.01</x:Double>
<x:Double>0.01</x:Double>
</x:Arguments>
</MapSpan>
</x:Arguments>
<maps:Map.ItemTemplate>
<DataTemplate x:DataType="model:LocationItem">
<maps:Pin Address="{Binding Address}"
Label="{Binding Name}"
Location="{Binding Location}">
</maps:Pin>
</DataTemplate>
</maps:Map.ItemTemplate>
</maps:Map>
</ContentPage>
UbicacionViewModel
的定义如下:
public partial class UbicacionViewModel : BaseViewModel
{
public ObservableCollection<LocationItem> Locations { get; set; }
public UbicacionViewModel()
{
Locations = new ObservableCollection<LocationItem>()
{
new LocationItem
{
Name = "My locatinon",
Address = "My street",
Location = new Location(-33, -70)
}
};
}
}
当我运行应用程序时,地图指向正确的位置,因为我硬编码了坐标。
如何动态获取这些坐标,其中纬度和经度来自集合中的第一个元素?
英文:
I have this XAML page:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:vm="clr-namespace:TimeApp.ViewModels"
x:Class="TimeApp.Views.UbicacionPage"
xmlns:maps="clr-namespace:Microsoft.Maui.Controls.Maps;assembly=Microsoft.Maui.Controls.Maps"
xmlns:sensors="clr-namespace:Microsoft.Maui.Devices.Sensors;assembly=Microsoft.Maui.Essentials"
xmlns:model="clr-namespace:TimeApp.Models"
x:DataType="vm:UbicacionViewModel">
<maps:Map x:Name="TheMap" ItemsSource="{Binding Locations}">
<x:Arguments>
<MapSpan>
<x:Arguments>
<sensors:Location Latitude="-33" Longitude="-70">
</sensors:Location>
<x:Double>0.01</x:Double>
<x:Double>0.01</x:Double>
</x:Arguments>
</MapSpan>
</x:Arguments>
<maps:Map.ItemTemplate>
<DataTemplate x:DataType="model:LocationItem">
<maps:Pin Address="{Binding Address}"
Label="{Binding Name}"
Location="{Binding Location}">
</maps:Pin>
</DataTemplate>
</maps:Map.ItemTemplate>
</maps:Map>
</ContentPage>
UbicacionViewModel
is defined this way:
public partial class UbicacionViewModel : BaseViewModel
{
public ObservableCollection<LocationItem> Locations { get; set; }
public UbicacionViewModel()
{
Locations = new ObservableCollection<LocationItem>()
{
new LocationItem
{
Name = "My locatinon",
Address = "My street",
Location = new Location(-33, -70)
}
};
}
}
When I run the application, the map points to the right point because I hardcoded the coordinates.
How can I get those coordinates dynamically where Latitude and Logitude comes from the first element in the collection?
答案1
得分: 2
在代码背后:
var pos = VM.Locations[0].Location;
var span = new MapSpan(pos, zoomx, zoomy);
TheMap.MoveToRegion(span);
因为 VisibleRegion
不可绑定,所以无法直接从 VM 或通过绑定执行此操作。
英文:
in the code behind
var pos = VM.Locations[0].Location;
var span = new MapSpan(pos,zoomx,zoomy);
TheMap.MoveToRegion(span);
because VisibleRegion
is not bindable you can't do this directly from the VM or via binding
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论