将地图移动到ObservableCollection的第一项。

huangapple go评论66阅读模式
英文:

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:

&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
			 xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
			 xmlns:vm=&quot;clr-namespace:TimeApp.ViewModels&quot;
			 x:Class=&quot;TimeApp.Views.UbicacionPage&quot;
             xmlns:maps=&quot;clr-namespace:Microsoft.Maui.Controls.Maps;assembly=Microsoft.Maui.Controls.Maps&quot;
             xmlns:sensors=&quot;clr-namespace:Microsoft.Maui.Devices.Sensors;assembly=Microsoft.Maui.Essentials&quot;
             xmlns:model=&quot;clr-namespace:TimeApp.Models&quot;
			 x:DataType=&quot;vm:UbicacionViewModel&quot;&gt;

    &lt;maps:Map x:Name=&quot;TheMap&quot; ItemsSource=&quot;{Binding Locations}&quot;&gt;
        &lt;x:Arguments&gt;
            &lt;MapSpan&gt;
                &lt;x:Arguments&gt;
                    &lt;sensors:Location Latitude=&quot;-33&quot; Longitude=&quot;-70&quot;&gt;
                    &lt;/sensors:Location&gt;
                    &lt;x:Double&gt;0.01&lt;/x:Double&gt;
                    &lt;x:Double&gt;0.01&lt;/x:Double&gt;
                &lt;/x:Arguments&gt;
            &lt;/MapSpan&gt;
        &lt;/x:Arguments&gt;
        &lt;maps:Map.ItemTemplate&gt;
            &lt;DataTemplate x:DataType=&quot;model:LocationItem&quot;&gt;
                &lt;maps:Pin Address=&quot;{Binding Address}&quot;
                          Label=&quot;{Binding Name}&quot;
                          Location=&quot;{Binding Location}&quot;&gt;
                &lt;/maps:Pin&gt;
            &lt;/DataTemplate&gt;
        &lt;/maps:Map.ItemTemplate&gt;
    &lt;/maps:Map&gt;
&lt;/ContentPage&gt;

UbicacionViewModel is defined this way:

public partial class UbicacionViewModel : BaseViewModel
{
    public ObservableCollection&lt;LocationItem&gt; Locations { get; set; }

    public UbicacionViewModel()
    {
        Locations = new ObservableCollection&lt;LocationItem&gt;()
        {
            new LocationItem
            {
                Name = &quot;My locatinon&quot;,
                Address = &quot;My street&quot;,
                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

huangapple
  • 本文由 发表于 2023年6月12日 07:07:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定