目前,我有一个项目使用Google Places API在UWP中显示附近的地方。我使用ListView来显示附近地点的数量,并成功地将API提供的一些基本信息显示到我的ListView中。我的DataTemplate这样将数据绑定到ListView以使用Google Places API和UWP显示到附近地点的距离
<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 Name="listPlace"
ItemTemplate="{StaticResource ResultPlaces}">
</ListView>
我解析JSON API中的结果在后面的代码,使之成为我ListView.ItemsSource ListView控件。问题是API不提供距离对象。因此,我创建了一种计算2个位置之间距离的方法,并使用它来计算API结果中的每个单独结果。我也创建了在提供API项目结果的Result类中获得名为placeDistance的set属性。
这是我得到设置属性
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);
}
当我部署到我的手机,在DataTemplate中显示的其他项目。但我无法获得任何距离文本。我做错什么了吗 ?
啊,我的错误。你是对的。我不应该创建一个新的实例。你的回答是我的一天,谢谢。 – hamdanjz4