我在网格视图下添加了一个地图控件来显示不同的位置,我想在每个地图上显示一个大头针或标记。我在列表中绑定了position对象下每个位置的经度和纬度。
下面是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:
<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; }
}
我还调试并获得了位置对象被初始化的经度和纬度,但这些地图上的每个引脚都不可见。
你的绑定表达式是错误的
<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>