private List <MarkerListViewItem> GetMarkersWithDistances(List <Marker> markersList) { List <MarkerListViewItem> list = new List <MarkerListViewItem>(); foreach (Marker marker in markersList) { var item = new MarkerListViewItem() { Marker = marker }; if (_currentLat != null && _currentLong != null) { /* * // Adapted from https://forums.xamarin.com/discussion/comment/34987/#Comment_34987 * double d = Math.Acos( * (Math.Sin((double)_currentLat) * Math.Sin(marker.Latitude)) + * (Math.Cos((double)_currentLat) * Math.Cos(marker.Latitude)) * Math.Cos(marker.Longitude - (double)_currentLong)); * * // in meters * //item.DistanceAway = (6378137 * d).ToString(); * // in miles * item.DistanceAway = (3959 * d).ToString(); */ Double distInMeters = DistanceHelper.DistanceInMetres((double)_currentLat, (double)_currentLong, marker.Latitude, marker.Longitude); Decimal distInMiles = (Decimal)(distInMeters * 0.000621371); Decimal distRounded = Decimal.Round(distInMiles, 1); item.DistanceAway = distRounded.ToString() + " mi"; item.DistanceAwayDecimal = distRounded; } else { item.DistanceAway = "Unknown"; item.DistanceAwayDecimal = 0; } list.Add(item); } return(list); }
async void ShowMarkersList() { _markersList = await _markerService.GetAllMarkers(); List <MarkerListViewItem> markersWithDistances = GetMarkersWithDistances(_markersList); // Create the ListView. _listView = new ListView { // Source of data items. ItemsSource = markersWithDistances, // Define template for displaying each item. // (Argument of DataTemplate constructor is called for // each item; it must return a Cell derivative.) ItemTemplate = new DataTemplate(() => { // Create views with bindings for displaying each property. Label nameLabel = new Label(); nameLabel.SetBinding(Label.TextProperty, "Marker.Title"); Label nodeIdLabel = new Label(); nodeIdLabel.SetBinding(Label.TextProperty, "DistanceAway"); /*nodeIdLabel.SetBinding(Label.TextProperty, * new Binding("DistanceAway", BindingMode.OneWay, * null, null, "Distance: {0}")); */ // Return an assembled ViewCell. return(new ViewCell { View = new StackLayout { Padding = new Thickness(0, 5), Orientation = StackOrientation.Horizontal, Children = { new StackLayout { VerticalOptions = LayoutOptions.Center, Spacing = 0, Children = { nameLabel, nodeIdLabel } } } } }); }) }; _listView.ItemTapped += async(sender, args) => { MarkerListViewItem marker = args.Item as MarkerListViewItem; if (marker == null) { return; } // TODO: Show marker detail screen //ShowMarkerLocation(marker.Title, "/?q=mobileapi/node/" + marker.NodeId); await Navigation.PushAsync(new MarkerInfoPage(marker.Marker)); _listView.SelectedItem = null; }; ContentGrid.Children.Add(_listView); }