将值绑定到必应地图上的纬度和经度

本文关键字:纬度 经度 地图 绑定 | 更新日期: 2023-09-27 18:04:53

我有一个包含Map的DataTemplate

<DataTemplate x:Key="MyMeetingsWithMapSquares">

        <Grid Width="350" Height="290" DataContext="{Binding}" >
            <StackPanel >
                <StackPanel.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0,1"  >
                        <GradientStop Color="#FF0072C6" Offset="0"/>
                        <GradientStop Color="#FF008FD4" Offset="1"/>
                    </LinearGradientBrush>
                </StackPanel.Background>
                <Grid DataContext="{Binding}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="90"/>
                        <RowDefinition Height="60"/>
                        <RowDefinition Height="50"/>
                        <RowDefinition Height="150"/>
                    </Grid.RowDefinitions>
                    <Border BorderBrush="#0166a0" BorderThickness="0,0,0,1" Margin="10,0,10,0">
                        <StackPanel Grid.Row="0">
                            <Grid DataContext="{Binding}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="220"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid Grid.Column="0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="50"/>
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Grid.Row="0" Margin="0,16,0,0" Text="{Binding MyMeetingSquareDayNumber}" Width="40" FontFamily="Segoe UI Semibold" FontSize="33" FontWeight="SemiBold" VerticalAlignment="Top"/>
                                        <TextBlock Grid.Row="0" Margin="0,16,0,0" Text="{Binding MyMeetingSquareMonthText}" Width="60" FontFamily="Segoe UI Regular" FontSize="33" FontWeight="Normal" VerticalAlignment="Top"/>
                                        <TextBlock Grid.Row="0" Margin="0,16,0,0" Text="{Binding MyMeetingSquareYearText}"  Width="60" FontFamily="Segoe UI Light" FontSize="33" FontWeight="Light" VerticalAlignment="Top" HorizontalAlignment="Left"/>
                                    </StackPanel>
                                    <TextBlock Grid.Row="1" Margin="0,5,0,0" Text="Business meeting" FontFamily="Segoe UI Semibold" FontSize="24" FontWeight="SemiBold" VerticalAlignment="Center"/>
                                </Grid>
                                <StackPanel Grid.Column="1" VerticalAlignment="Center">
                                    <TextBlock Text="{Binding MyMeetingSquareDayHour}" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,10,0"/>
                                </StackPanel>
                            </Grid>
                        </StackPanel>
                    </Border>
                    <Border Grid.Row="1" BorderBrush="#0166a0" BorderThickness="0,0,0,1" Margin="10,0,10,0">
                        <StackPanel >
                            <TextBlock Text="{Binding MyMeetingSquareSummary}" FontFamily="Segoe UI Light" FontSize="16" Margin="0,10,0,10" TextWrapping="Wrap"/>
                        </StackPanel>
                    </Border>
                    <StackPanel Grid.Row="2">
                        <Border BorderBrush="#0166a0" BorderThickness="0,0,0,1" Margin="10,0,10,0">
                            <ScrollViewer VerticalScrollBarVisibility="Hidden" VerticalScrollMode="Disabled" HorizontalScrollBarVisibility="Hidden" HorizontalScrollMode="Enabled">
                                <GridView ItemContainerStyle="{StaticResource GridViewItemStyle2}" ItemsSource="{Binding MyMeetingsSquareUsers}"  ItemTemplateSelector="{StaticResource meetingSelector}" Grid.Row="1" Margin="0,10,0,0"  SelectionMode="None" HorizontalContentAlignment="Left" VerticalContentAlignment="Bottom">
                                    <GridView.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <ItemsWrapGrid Orientation="Vertical" MaximumRowsOrColumns="1"/>
                                        </ItemsPanelTemplate>
                                    </GridView.ItemsPanel>
                                </GridView>
                            </ScrollViewer>
                        </Border>
                    </StackPanel>
                    <StackPanel Grid.Row="3" Height="150" DataContext="{Binding}">
                        <Maps:Map Tag="{Binding MyMeetingSquareLat}"  Margin="0,10,0,0"  DoubleTappedOverride="Map_DoubleTappedOverride" ShowBreadcrumb="False" Height="150" ShowNavigationBar="False"  ShowScaleBar="False" ShowTraffic="False" Width="350" Credentials="AnZKLHgAfKSwa5BAB2Kr-g8KENJBm1US3txxxxxxzCDUAgxhgJZVXyUgRIwM" ViewChanged="Map_ViewChanged" >
                            <Maps:Map.Center>
                                <Maps:Location Latitude="{Binding MyMeetingSquareLat}" Longitude="{Binding MyMeetingSquareLng}"/>
                            </Maps:Map.Center>
                        </Maps:Map>
                    </StackPanel>
                </Grid>
            </StackPanel>
        </Grid>
    </DataTemplate>

我试图将值绑定到它的纬度和经度,但由于某种原因,它们总是0。我尝试将其中一个值绑定到Map标签,它工作了,标签是49.597378,但纬度是0。

如何将值传递给地图lat和LNG ?

编辑

所以我已经改变了我的代码中的一些东西,并添加了一个图钉

<StackPanel Grid.Row="3" Height="150" DataContext="{Binding}">
                        <Maps:Map Tag="{Binding MyMeetingSquareLat}" DataContext="{Binding}" ZoomLevel="5"  Margin="0,10,0,0"  DoubleTappedOverride="Map_DoubleTappedOverride" ShowBreadcrumb="False" Height="150" ShowNavigationBar="False"  ShowScaleBar="False" ShowTraffic="False" Width="350" Credentials="AnZKLHgAfKSwa5BAB2Kr-g8KENJBmxxxxxxyOCEwbJzCDUAgxhgJZVXyUgRIwM" ViewChanged="Map_ViewChanged" >
                            <Maps:Map.Center>
                                <Maps:Location Latitude="{Binding MyMeetingSquareLat}" Longitude="{Binding MyMeetingSquareLng}"/>
                            </Maps:Map.Center>
                            <Maps:Map.Children>
                                <Maps:Pushpin >
                                    <Maps:Pushpin.Background>
                                        <ImageBrush Stretch="Fill" ImageSource="ms-appx:///Assets/Pdf/edit.png"/>
                                    </Maps:Pushpin.Background>
                                    <Maps:MapLayer.Position>
                                        <Maps:Location Latitude="{Binding MyMeetingSquareLat}" Longitude="{Binding MyMeetingSquareLng}" />
                                    </Maps:MapLayer.Position>
                                </Maps:Pushpin>
                            </Maps:Map.Children>
                        </Maps:Map>
                    </StackPanel>
                </Grid>
            </StackPanel>

图钉出现在正确的位置,但地图中心继续移动到0 0点,由于某种原因,它没有读取绑定的值

将值绑定到必应地图上的纬度和经度

您正在将字符串(如您在评论中所说)绑定到Maps:LocationLatitude属性。然而,Maps:Location期望此值为double

您需要使用以下代码将该值转换为双精度类型:

Double.Parse(myStringValue);

如果值MyMeetingSquareLat本身不能更改为字符串,则添加另一个像这样的属性,并绑定到它:

public Double MyMeetingSquareLatDouble
{
    get
    {
        return Double.Parse(this.MyMeetingSquareLat);
    }
}