【问题标题】:Showing Marker on list of maps added in list view in Xamarin forms在 Xamarin 表单的列表视图中添加的地图列表上显示标记
【发布时间】:2021-09-09 19:06:54
【问题描述】:

我在网格视图下添加了一个地图控件来显示不同的位置,我想在每个地图上显示一个图钉或标记。我在列表中绑定了位置对象下每个位置的经度和纬度。

以下是 XAML:

<grial:GridView
            
        Grid.Row="1"
        ColumnSpacing="15"
        RowSpacing="15"
        ColumnCount="{
                    grial:OnOrientationInt 
                        PortraitPhone=1,
                        LandscapePhone=2,

                        PortraitTablet=2,
                        LandscapeTablet=2,
                    
                        PortraitDesktop=3,
                        LandscapeDesktop=3
                }"
    VerticalOptions="FillAndExpand"
    ItemsSource="{ Binding Branches }">
            <grial:GridView.ItemTemplate>
                <DataTemplate>
                    <local:ArticleColumnItemTemplate>
                    </local:ArticleColumnItemTemplate>
                </DataTemplate>
            </grial:GridView.ItemTemplate>
        </grial:GridView>

下面是内容视图:

<ContentView.Content>
    <Grid
        Padding="0">
        <grial:CardView
            BackgroundColor="White"
                Padding="10"
                CornerRadius="15"
                RowSpacing="5"
                Margin="{ 
                    grial:OnOrientationThickness
                        Default='5,5,5,5',
                        LandscapePhone='5,5,5,5'
                }">
            <grial:CardView.RowDefinitions>
                <RowDefinition
                Height="Auto" />

            </grial:CardView.RowDefinitions>

            <maps:Map x:Name="map"
                    HeightRequest="280"
                    MapType="Satellite"                    
                    Grid.Row="0">
                <maps:Map.Pins>
                    <maps:Pin
                            
                            Address="{Binding Branches.address.street1}"
                            Label="Label1"
                            Position="{Binding Branches.Position}"
                            Type="Place">
                    </maps:Pin>
                </maps:Map.Pins>
            </maps:Map>
        </grial:CardView>

    </Grid>
</ContentView.Content>

下面是我在页面加载时绑定的视图模型

public class MapPageViewModel : INotifyPropertyChanged
{
private readonly string _variantPageName;
public event PropertyChangedEventHandler PropertyChanged;

public List<Branches> _branches;
public List<Branches> Branches
{
    get => _branches;
    set
    {
        _branches = value;

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("Branches"));
        }
    }
}

public MapPageViewModel()
{
    LoadData();
}

public async void LoadData()
{
    Branches = await GetApiCalls.ServiceClientInstance.GetBranches();

    foreach(Branches item in Branches)
    {
        item.Position = new Position(Convert.ToDouble(item.address.location.latitude),
                                    Convert.ToDouble(item.address.location.longitude));
    }
}
}

以下是分支和地址类:

public class Branches : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    
    public string branchName { get; set; }

    public Address address { get; set; }

    public Position _position;

    public Position Position
    {
        get => _position;
        set
        {
            _position = value;

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Position"));
            }
        }
    }
}

public class Address
{
    public string street1 { get; set; }
    public string street2 { get; set; }
    public Location location { get; set; }
}

public class Location
{
    public double? latitude { get; set; }
    public double? longitude { get; set; }
}

我还调试并获得了使用经度和纬度启动的位置对象,但在这些地图中的每一个上都看不到图钉。

【问题讨论】:

    标签: google-maps xamarin xamarin.forms maps


    【解决方案1】:

    你的绑定表达式错误

    <maps:Pin Address="{Binding Branches.address.street1}"
              Label="Label1"
              Position="{Binding Branches.Position}"
              Type="Place">
    </maps:Pin>
    

    应该是

    <maps:Pin Address="{Binding address.street1}"
              Label="Label1"
              Position="{Binding Position}"
              Type="Place">
    </maps:Pin>
    

    【讨论】:

    • 尝试将值绑定到标签以验证它们是否正确绑定
    • 当我将任何内容绑定到标签时,它会给我一个错误,即标签不能为空。当我调试时,我看到值被分配给变量。
    • 这是编译器错误还是运行时异常? 具体信息是什么?
    • 这是一个运行时错误,应用程序崩溃并出现 System.ArgumentException: 'Pin must have a Label to be added to a map'
    • 我认为你不明白我的建议。我的意思是在你的ContentView 中放置一个Label 元素并尝试绑定到它。这将告诉您问题是一般绑定问题还是特定于地图的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    相关资源
    最近更新 更多