示例#1
0
        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);
        }