Пример #1
0
        public GeoLocation(GeoCoordinate coordinate, Double heading, Double speed, CivicAddress address, DateTimeOffset timestamp)
        {
            if (coordinate == null)
            {
                throw new ArgumentNullException("coordinate");
            }

            if (address == null)
            {
                throw new ArgumentNullException("address");
            }

            if (heading < 0.0 || heading > 360.0)
            {
                throw new ArgumentOutOfRangeException("heading", SR2.GetString(SR.Argument_MustBeInRangeZeroTo360));
            }

            if (speed < 0.0)
            {
                throw new ArgumentOutOfRangeException("speed", SR2.GetString(SR.Argument_MustBeNonNegative));
            }

            Coordinate = coordinate;
            Address    = address;
            Heading    = heading;
            Speed      = speed;
            Timestamp  = timestamp;
        }
        public GeoCoordinate(Double latitude, Double longitude, Double horizontalAccuracy, Double altitude, Double verticalAccuracy)
        {
            if (Double.IsNaN(latitude) || latitude > 90.0 || latitude < -90.0)
            {
                throw new ArgumentOutOfRangeException("latitude", SR2.GetString(SR.Argument_MustBeInRangeNegative90to90));
            }

            if (Double.IsNaN(longitude) || longitude > 180.0 || longitude < -180.0)
            {
                throw new ArgumentOutOfRangeException("longitude", SR2.GetString(SR.Argument_MustBeInRangeNegative180To180));
            }

            if (horizontalAccuracy < 0.0)
            {
                throw new ArgumentOutOfRangeException("horizontalAccuracy", SR2.GetString(SR.Argument_MustBeNonNegative));
            }

            if (verticalAccuracy < 0.0)
            {
                throw new ArgumentOutOfRangeException("verticalAccuracy", SR2.GetString(SR.Argument_MustBeNonNegative));
            }

            horizontalAccuracy = (horizontalAccuracy == 0.0) ? Double.NaN : horizontalAccuracy;
            verticalAccuracy   = (verticalAccuracy == 0.0) ? Double.NaN : verticalAccuracy;

            Latitude           = latitude;
            Longitude          = longitude;
            Altitude           = altitude;
            HorizontalAccuracy = horizontalAccuracy;
            VerticalAccuracy   = verticalAccuracy;
        }
Пример #3
0
        public CivicAddress(String addressLine1, String addressLine2, String building, String city, String countryRegion, String floorLevel, String postalCode, String stateProvince)
            : this()
        {
            bool hasField = false;

            if (addressLine1 != null && addressLine1 != String.Empty)
            {
                hasField     = true;
                AddressLine1 = addressLine1;
            }
            if (addressLine2 != null && addressLine2 != String.Empty)
            {
                hasField     = true;
                AddressLine2 = addressLine2;
            }
            if (building != null && building != String.Empty)
            {
                hasField = true;
                Building = building;
            }
            if (city != null && city != String.Empty)
            {
                hasField = true;
                City     = city;
            }
            if (countryRegion != null && countryRegion != String.Empty)
            {
                hasField      = true;
                CountryRegion = countryRegion;
            }
            if (floorLevel != null && floorLevel != String.Empty)
            {
                hasField   = true;
                FloorLevel = floorLevel;
            }
            if (postalCode != null && postalCode != String.Empty)
            {
                hasField   = true;
                PostalCode = postalCode;
            }

            if (stateProvince != null && stateProvince != String.Empty)
            {
                hasField      = true;
                StateProvince = stateProvince;
            }

            if (!hasField)
            {
                throw new ArgumentException(SR2.GetString(SR.Argument_RequiresAtLeastOneNonEmptyStringParameter));
            }
        }
        public Double GetDistanceTo(GeoCoordinate other)
        {
            //  The Haversine formula according to Dr. Math.
            //  http://mathforum.org/library/drmath/view/51879.html

            //  dlon = lon2 - lon1
            //  dlat = lat2 - lat1
            //  a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
            //  c = 2 * atan2(sqrt(a), sqrt(1-a))
            //  d = R * c

            //  Where
            //    * dlon is the change in longitude
            //    * dlat is the change in latitude
            //    * c is the great circle distance in Radians.
            //    * R is the radius of a spherical Earth.
            //    * The locations of the two points in
            //        spherical coordinates (longitude and
            //        latitude) are lon1,lat1 and lon2, lat2.

            if (Double.IsNaN(Latitude) || Double.IsNaN(Longitude) ||
                Double.IsNaN(other.Latitude) || Double.IsNaN(other.Longitude))
            {
                throw new ArgumentException(SR2.GetString(SR.Argument_LatitudeOrLongitudeIsNotANumber));
            }

            double dDistance = Double.NaN;

            double dLat1 = Latitude * (Math.PI / 180.0);
            double dLon1 = Longitude * (Math.PI / 180.0);
            double dLat2 = other.Latitude * (Math.PI / 180.0);
            double dLon2 = other.Longitude * (Math.PI / 180.0);

            double dLon = dLon2 - dLon1;
            double dLat = dLat2 - dLat1;

            // Intermediate result a.
            double a = Math.Pow(Math.Sin(dLat / 2.0), 2.0) +
                       Math.Cos(dLat1) * Math.Cos(dLat2) *
                       Math.Pow(Math.Sin(dLon / 2.0), 2.0);

            // Intermediate result c (great circle distance in Radians).
            double c = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1.0 - a));

            // Distance.
            const Double kEarthRadiusMs = 6376500;

            dDistance = kEarthRadiusMs * c;

            return(dDistance);
        }