使用 UWP Google Places API 将数据绑定到 ListView 以显示到附近地点的距离

Binding data to ListView to display distance to nearby place using Google Places API with UWP

目前,我有一个项目使用 Google 个地点 API 通过 UWP 显示附近的地点。我正在使用 ListView 显示附近地点的数量,并成功地将 API 提供的一些基本信息显示到我的 ListView 中。我有这样的 DataTemplate

<DataTemplate x:Key="ResultPlaces">
        <StackPanel Orientation="Vertical">

            ...
                <Grid Grid.Column="2">
                    <TextBlock Text="{Binding placeDistance}" Foreground="#42424c" TextWrapping="Wrap" FontSize="12" Margin="10,0,0,0"/>

                </Grid>
            </Grid>


        </StackPanel>

我也有这样的ListView

<ListView Name="listPlace"
                    ItemTemplate="{StaticResource ResultPlaces}">

                </ListView>

我已经在后面的代码中解析 API 结果中的 JSON 并将其作为我的 ListView.ItemsSource。问题是 API 不提供距离对象。因此,我创建了一种方法来计算 2 个地方之间的距离,并用它来计算 API 个结果中的每个结果。我还创建了 get set 属性,在 class 结果中调用了 placeDistance,提供了 API 项结果。

这是我的设置 属性

  public class Result
    {
        ....
        public Review[] reviews { get; set; }
        public int user_ratings_total { get; set; }

        public string placeDistance { get; set; }
    }

这是我的代码,用于计算结果中的每个距离

int lengthResult = placesList.results.Count();


                for (int i = 0; i < lengthResult; i++)
                {
                    double myLat = placesList.results[i].geometry.location.lat;
                    double myLong = placesList.results[i].geometry.location.lng;
                    double myCurrentLat = Convert.ToDouble(parameters.lat);
                    double myCurrentLong = Convert.ToDouble(parameters.longit);

                    var newDistance = new Result();
                    newDistance.placeDistance = DistanceBetweenPlaces(myCurrentLong, myCurrentLat, myLong, myLat);


                }

当我将它部署到我的 phone 中时,DataTemplate 中的其他项目显示正确。但我无法获得任何距离文本。我是不是做错了什么?

在没有看到完整代码的情况下很难下定论,但我最好的猜测是您不应该在计算距离的循环中创建 Result 的新实例。

从命名来看,我假设 placesList.results 已经是一个包含 Result 的列表以及您要绑定到 ListView 的所有其他项。在这种情况下,您应该替换:

var newDistance = new Result();
newDistance.placeDistance =
    DistanceBetweenPlaces(myCurrentLong, myCurrentLat, myLong, myLat);

与:

placesList.results[i].placeDistance = 
    DistanceBetweenPlaces(myCurrentLong, myCurrentLat, myLong, myLat);