using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace VECV_WebApi.Models.Util { public class DistanceCalc { /// /// take lat/long in double datatype and return decimal list of x,y,z, coordinates respectively /// /// /// /// public Dictionary GetCoordinates(double latitude, double longitude) { //Dictionary Coordinates= new Dictionary(); Dictionary Coordinates = new Dictionary(); //decimal XCoordinate,YCoordinate,ZCoordinate; //XCoordinate = Convert.ToDecimal(6371000 * Convert.ToDouble((Math.Cos(latitude)) * (Math.Cos(longitude)))); //YCoordinate = Convert.ToDecimal(6371000 * Convert.ToDouble((Math.Cos(latitude)) * (Math.Sin(longitude)))); //ZCoordinate = Convert.ToDecimal(6371000 * Convert.ToDouble((Math.Sin(latitude)))); latitude = Math.PI * latitude / 180; longitude = Math.PI * longitude / 180; double xCoordinate = 6371000d * Convert.ToDouble((Math.Cos(latitude)) * (Math.Cos(longitude))); double yCoordinate = 6371000d * Convert.ToDouble((Math.Cos(latitude)) * (Math.Sin(longitude))); double zCoordinate = 6371000d * Convert.ToDouble((Math.Sin(latitude))); Coordinates.Add("XCoordinate", xCoordinate); Coordinates.Add("YCoordinate", yCoordinate); Coordinates.Add("ZCoordinate", zCoordinate); return Coordinates; } /// /// This method simply return unit distance of the vehicle's current position from geofence boundry /// /// /// I point X coordinate /// I point Y coordinate /// J point X coordinate /// J point Y coordinate /// Current position X coordinate /// Current position Y coordinate /// public decimal CalcDistance(double IXCoordinate, double IYCoordinate, double JXCoordinate, double JYCoordinate, double XCoordinate, double YCoordinate) { try { double _constantA, _constantB; double _slopA, _slopB; double _tempexprA, _tempexprB; double _intersectX, _intersectY; _slopA = (JYCoordinate - IYCoordinate) / (JXCoordinate - IXCoordinate); _constantA = IYCoordinate - (_slopA * IXCoordinate); _slopB = -(1 / _slopA); _constantB = YCoordinate - (_slopB * XCoordinate); _tempexprA = (_slopA) - (_slopB); _tempexprB = _constantA - (_constantB); if (_tempexprB == 0) { _intersectX = 0; } else { _intersectX = -(_tempexprA / _tempexprB); } _intersectY = (_slopB * _intersectX) + (_constantB); return Convert.ToDecimal(Math.Sqrt(Math.Pow(Convert.ToDouble(_intersectX - XCoordinate), 2) + Math.Pow(Convert.ToDouble(_intersectY - YCoordinate), 2))); } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// This fun use to get distance between two lat and long. /// /// location latitude /// location longitude /// distance public double DistanceInKM(double[] latitude, double[] longitude) { try { double distance = 0.0F; double lat1, lng1, lat2, lng2; double Radius = 6371; double LatDistance, LngDistance; for (int i = 0; i < latitude.Length - 1; i++) { lat1 = latitude[i] * Math.PI / 180; lat2 = latitude[i + 1] * Math.PI / 180; lng1 = longitude[i] * Math.PI / 180; lng2 = longitude[i + 1] * Math.PI / 180; LatDistance = lat2 - lat1; LngDistance = lng2 - lng1; var a = Math.Sin(LatDistance / 2) * Math.Sin(LatDistance / 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin(LngDistance / 2) * Math.Sin(LngDistance / 2); var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); distance = distance + Radius * c; } return distance; } catch (Exception ex) { throw new Exception(ex.Message); } } } }