コード例 #1
0
 public Geolocation(Geolocation otherPoint)
 {
     Latitude  = otherPoint.Latitude;
     Longitude = otherPoint.Longitude;
 }
コード例 #2
0
        public decimal GreatCircleDistance(Geolocation other, LengthTypes lengthType = LengthTypes.Meters)
        {
            #region //Variables
            double        lat1, lat2, lng1, lng2;
            double        latDiff, lngDiff;
            double        sin2Lat, sin2Lng;
            double        a, arcLength;
            decimal?      distance                  = null;
            decimal?      convertedDistance         = null;
            const decimal EarthRadius               = 6371000;
            const decimal KilometersConversionRatio = 0.001M;
            const decimal FeetConversionRatio       = 3.2808399M;
            const decimal MilesConversionRatio      = 0.00062137119M;
            #endregion


            #region                                    //Step1
            /*Convert from degrees to radians*/
            lng1 = ToRadians((double)Longitude);       //The 'x' of city
            lat1 = ToRadians((double)Latitude);        //The 'y' of city

            lng2 = ToRadians((double)other.Longitude); //The 'x' of otherCity
            lat2 = ToRadians((double)other.Latitude);  //The 'y' of otheCity
            #endregion                                 //End of: Step1

            #region                                    //Step2
            /*Calculate for 'latDiff' and 'lngDiff' */
            latDiff = lat1 - lat2;
            lngDiff = lng1 - lng2;
            #endregion//End of: Step2


            #region//Step3
            /*Calculate for sin^2() portion of formula*/
            sin2Lat = Math.Pow(Math.Sin(latDiff / 2), 2);
            sin2Lng = Math.Pow(Math.Sin(lngDiff / 2), 2);

            #endregion //End of: Step3

            #region    //Step4
            /*Calculate 'a' where 'a' is the result of the rest of the terms under the radical*/
            a = sin2Lat + Math.Cos(lat1) * (Math.Cos(lat2)) * sin2Lng;
            #endregion //End of: Step4

            #region    //Step5
            /*Calculate for 'arcLength'*/
            arcLength = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
            #endregion //End of: Step5

            #region    //Step6
            /*Calculate for 'distance'*/
            distance = (decimal)arcLength * (decimal)EarthRadius;
            #endregion //End of: Step6

            #region    //Step7
            if (lengthType == LengthTypes.Meters)
            {
                return((decimal)distance);
            }
            else
            {
                switch (lengthType)
                {
                case LengthTypes.Kilometers:
                    convertedDistance = (decimal)distance * KilometersConversionRatio;
                    break;

                case LengthTypes.Feet:
                    convertedDistance = (decimal)distance * FeetConversionRatio;
                    break;

                case LengthTypes.Miles:
                    convertedDistance = (decimal)distance * MilesConversionRatio;
                    break;
                }
                if (!String.IsNullOrWhiteSpace(convertedDistance.ToString()))
                {
                    distance = (decimal)convertedDistance;
                }
                //Console.WriteLine($"distance: {distance}");
                return((decimal)distance);
            }
            #endregion//End of: Step7
        }