Currently, I have a project to display nearby places with UWP using Google Places API. I'm using ListView to display the numbers of nearby places and did successfully to display some basic information provided by API into my ListView. I have DataTemplate like this
<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>
And i also have the ListView like this
<ListView Name="listPlace"
ItemTemplate="{StaticResource ResultPlaces}">
</ListView>
I've parsing the JSON in result of API in code behind and make it to be my ListView.ItemsSource. The problem is the API don't provide distance object. So, I created a method to calculte distance between 2 places and use it to calculate every single result in API results. I'm also created get set property called placeDistance in class Result that provided API items result.
This my get set property
public class Result
{
....
public Review[] reviews { get; set; }
public int user_ratings_total { get; set; }
public string placeDistance { get; set; }
}
And this my code to calculate every single distance on 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);
}
When I deployed it into my phone, the other items in DataTemplate display correctly. But I couldn't get any distance text. Did I do something wrong ?
It's difficult to say for certain without seeing the full code, but my best guess is that you shouldn't create a new instance of Result
in the loop, where you are calculating the distance.
Judging from the naming, I would assume that placesList.results
is already a list of Result
s with all the other items, which you are binding to the ListView
. In this case, you should replace:
var newDistance = new Result();
newDistance.placeDistance =
DistanceBetweenPlaces(myCurrentLong, myCurrentLat, myLong, myLat);
with:
placesList.results[i].placeDistance =
DistanceBetweenPlaces(myCurrentLong, myCurrentLat, myLong, myLat);