public static bool LocationIsInDistance(Wgs84Location pointForCheck, Wgs84Location center, double radius)
 {
     if (GetDistanceBetweenTwoPoints(pointForCheck, center) <= radius)
         return true;
     else
         return false;
 }
        public static Wgs84Location CalculateDerivedPosition(Wgs84Location point, double range, double bearing)
        {
            const double EarthRadius = 6371000; // m

            double latA = DegreeToRadian(point.Latitude);
            double lonA = DegreeToRadian(point.Longitude);

            double angularDistance = range / EarthRadius;
            double trueCourse = DegreeToRadian(bearing);

            double lat = Math.Asin(
                    Math.Sin(latA) * Math.Cos(angularDistance) +
                            Math.Cos(latA) * Math.Sin(angularDistance)
                            * Math.Cos(trueCourse));

            double dlon = Math.Atan2(
                    Math.Sin(trueCourse) * Math.Sin(angularDistance)
                            * Math.Cos(latA),
                    Math.Cos(angularDistance) - Math.Sin(latA) * Math.Sin(lat));

            double lon = ((lonA + dlon + Math.PI) % (Math.PI * 2)) - Math.PI;

            lat = RadianToDegree(lat);
            lon = RadianToDegree(lon);

            var newPoint = new Wgs84Location((float) lon, (float) lat);
            return newPoint;

        }
        protected async void ContinueNearestSearchOnUiThread()
        {
            Haltestellen = null;
            NotifyOfPropertyChange(() => Haltestellen);

            var posResult = await _locationService.GetCurrentPosition();

            if (posResult.Succeeded)
            {
                var pos = posResult.Position;
                InfoMessage = String.Format("{0}: {1:F2} {2:F2}", AppResources.PositionMessage_YourPosition, pos.Coordinate.Point.Position.Longitude, pos.Coordinate.Point.Position.Latitude);

                MyLocation = new Wgs84Location(pos.Coordinate);

                var haltestellen = await _dataService.GetNearestHaltestellenAsync(MyLocation);

                if (!haltestellen.Any())
                {
                    InfoMessage = String.Format("{0}: {1:F2} {2:F2}", AppResources.PositionMessage_NoStopsFoundNear,
                        pos.Coordinate.Point.Position.Longitude, pos.Coordinate.Point.Position.Latitude);
                }
                else
                {
                    Haltestellen = new BindableCollection<Haltestelle>(haltestellen.OrderBy(h => h.Distanz));
                    NotifyOfPropertyChange(() => Haltestellen);
                }
            }
            else
            {
                InfoMessage = posResult.ErrorMessage;
            }
        }
        public static double GetDistanceBetweenTwoPoints(Wgs84Location p1, Wgs84Location p2)
        {
            const double R = 6371000; // m

            double dLat = DegreeToRadian(p2.Latitude - p1.Latitude);
            double dLon = DegreeToRadian(p2.Longitude - p1.Longitude);
            double lat1 = DegreeToRadian(p1.Latitude);
            double lat2 = DegreeToRadian(p2.Latitude);

            double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2)
                    * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2);
            double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            double d = R * c;

            return d;
        }
 public static double GetDistanceBetweenTwoPoints(Wgs84Location p1, double p2Lon, double p2Lat)
 {
     return GetDistanceBetweenTwoPoints(p1, new Wgs84Location(p2Lon, p2Lat));
 }
        //
        // http://stackoverflow.com/questions/3695224/android-sqlite-getting-nearest-locations-with-latitude-and-longitude/12997900#12997900
        //
        public async Task<List<Haltestelle>> GetNearestHaltestellenAsync(Wgs84Location center, double radius = 500.0)
        {
            try
            {
                double mult = 1; // mult = 1.1; is more reliable

                var p1 = NearestLocationHelpers.CalculateDerivedPosition(center, mult * radius, 0);
                var p2 = NearestLocationHelpers.CalculateDerivedPosition(center, mult * radius, 90);
                var p3 = NearestLocationHelpers.CalculateDerivedPosition(center, mult * radius, 180);
                var p4 = NearestLocationHelpers.CalculateDerivedPosition(center, mult * radius, 270);

                var db = GetReferenceDataContext();
                var haltestellen = await db.GetNearestHaltestellenAsync(p3.Latitude, p1.Latitude, p2.Longitude, p4.Longitude).ConfigureAwait(false);

                foreach (var h in haltestellen)
                {
                    h.Distanz = NearestLocationHelpers.GetDistanceBetweenTwoPoints(center, h.Longitude, h.Latitude);
                }

                return haltestellen;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }

            return new List<Haltestelle>();
        }