public static double Distance(GeoPoint point1, GeoPoint point2) { var lat1 = point1.Latitude; var lat2 = point2.Latitude; var lon1 = point1.Longtitude; var lon2 = point2.Longtitude; var R = 6371000000; // m (change this constant to get miles) var dLat = (lat2 - lat1) * Math.PI / 180; var dLon = (lon2 - lon1) * Math.PI / 180; var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(lat1 * Math.PI / 180) * Math.Cos(lat2 * Math.PI / 180) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2); var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); var d = R * c; // The current result is in mm, division is done to return meters return Math.Round(d) / 1000; }
public List<Parking> GetParkingsByBusiness(string businessId, string businessType) { long id; if (!long.TryParse(businessId, out id)) { WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest; return null; } List<Parking> parkings; using (var dataAccess = new ParkingDataAccess()) { parkings = dataAccess.GetParkingByBusiness(id).ToList(); } if (!parkings.Any()) { using (var query = FacebookQueryFactory.CreateQuery()) { var dictionary = query.Select("latitude", "longitude").From(FacebookTable.Place).WhereId(id).Execute(); if (dictionary == null) { WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound("Business not found"); return null; } var geo = new GeoPoint(double.Parse(dictionary["longitude"]), double.Parse(dictionary["latitude"])); using (var dataAccess = new ParkingDataAccess()) { parkings = dataAccess.GetParkingsByGeo(geo, 1000, 10).ToList(); var business = new Business() { BusinessType = BusinessType.Facebook, FacebookId = id, }; parkings.ForEach(business.RecommendedParkings.Add); dataAccess.AddBusiness(business); } } } return parkings; }
public List<Parking> GetParkingsByGeo(string latitude, string longtitude, string radius) { // Convert the request parameters into the right type double longt; double lat; double rad; if (!double.TryParse(longtitude, out longt) || !double.TryParse(latitude, out lat) || !double.TryParse(radius, out rad)) { // The parameters aren't of the right type, close the request WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest; return null; } var point = new GeoPoint(longt, lat); try { List<Parking> parkings; using (var dataAccess = new ParkingDataAccess()) { parkings = dataAccess.GetParkingsByGeo(point, rad).ToList(); } return parkings; } catch (Exception ex) { string message = string.Empty; while (ex != null) { message = string.Format("{0}\nType:{1}\nMessage:{2}", message, ex.GetType().Name, ex.Message); ex = ex.InnerException; } return new List<Parking>() { new Parking() { Name = message } }; } }
/// <summary> /// Calculates the distance between two points /// </summary> /// <param name="point"></param> /// <returns></returns> public double Distance(GeoPoint point) { return Distance(this, point); return Math.Sqrt(Math.Pow((Longtitude - point.Longtitude), 2) + Math.Pow((Latitude - point.Latitude), 2)); }