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

hamdanjz4

目前,我有一个项目,可以使用Google Places 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结果中的每个结果。我还在提供API项结果的Result类中创建了名为placeDistance的get set属性。

这是我的设置属性

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

        public string placeDistance { get; set; }
    }

这是我的代码,用于计算Result上的每个距离

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);


                }

当我将其部署到手机中时,DataTemplate中的其他项目会正确显示。但我无法获得任何距离文字。我做错什么了吗 ?

达米尔·阿尔(Damir Arh)

没有完整的代码很难确定,但是我最大的猜测是您不应该Result在循环中创建新的实例来计算距离。

从命名的角度来看,我认为该placesList.results列表已经是Results的列表,其中包含所有其他项目,您将这些项目绑定到ListView在这种情况下,您应该替换:

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

和:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章