Exemplo n.º 1
0
        public async Task <string> GetTimeForPoint(Tuple <double, double> pointA, DateTimeOffset dateTimeOffset)
        {
            var azureMaps = new AzureMapsToolkit.AzureMapsServices(GetMapKey());

            if (azureMaps == null)
            {
                logger.LogError("Failed to get instance of azuremaps.");
                throw new Exception("Failed to get instance of azuremaps");
            }

            var timezoneRequest = new TimeZoneRequest
            {
                Query     = $"{pointA.Item1},{pointA.Item2}",
                TimeStamp = dateTimeOffset.UtcDateTime.ToString("O")
            };

            logger.LogInformation("Getting time for timezoneRequest: {0}", JsonSerializer.Serialize(timezoneRequest));

            var response = await azureMaps.GetTimezoneByCoordinates(timezoneRequest).ConfigureAwait(false);

            logger.LogInformation("Response from getting time for timezoneRequest: {0}", JsonSerializer.Serialize(response));

            if (response.HttpResponseCode != (int)HttpStatusCode.OK && response.HttpResponseCode != 0)
            {
                throw new Exception($"Error getting timezonebycoordinates: {response}");
            }

            return(response?.Result?.TimeZones[0]?.ReferenceTime?.WallTime);
        }
Exemplo n.º 2
0
        public async Task <double> GetDistanceBetweenTwoPoints(Tuple <double, double> pointA, Tuple <double, double> pointB, bool IsMetric = true)
        {
            var azureMaps       = new AzureMapsToolkit.AzureMapsServices(GetMapKey());
            var distanceRequest = new GreatCircleDistanceRequest
            {
                Query = $"{pointA.Item1},{pointA.Item2}:{pointB.Item1},{pointB.Item2}",
                Start = new Coordinate()
                {
                    Lat = pointA.Item1, Lon = pointA.Item2
                },
                End = new Coordinate()
                {
                    Lat = pointB.Item1, Lon = pointB.Item2
                }
            };

            logger.LogInformation("Getting distance between two points: {0}", JsonSerializer.Serialize(distanceRequest));

            var response = await azureMaps.GetGreatCircleDistance(distanceRequest).ConfigureAwait(false);

            logger.LogInformation("Response from getting distance between two points: {0}", JsonSerializer.Serialize(response));

            try
            {
                if (response.HttpResponseCode != (int)HttpStatusCode.OK && response.HttpResponseCode != 0)
                {
                    throw new Exception($"Error getting GetGreatCircleDistance: {JsonSerializer.Serialize(response)}");
                }

                var distanceInMeters = (long)response.Result.Result.DistanceInMeters;

                logger.LogInformation("Distance in Meters: {0}", distanceInMeters);

                if (IsMetric)
                {
                    var res = distanceInMeters / MetersPerKilometer;
                    logger.LogInformation("Kilometers : {0}", res);
                    return(res);
                }
                else
                {
                    var res = distanceInMeters / MetersPerMile;
                    logger.LogInformation("Miles : {0}", res);
                    return(res);
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Exception encountered");
                throw;
            }
        }
Exemplo n.º 3
0
        public async Task <Address> GetAddress(double latitude, double longitude)
        {
            var azureMaps = new AzureMapsToolkit.AzureMapsServices(GetMapKey());

            if (azureMaps == null)
            {
                logger.LogError("Failed to get instance of azuremaps.");
                throw new Exception("Failed to get instance of azuremaps");
            }

            var searchAddressReverseRequest = new SearchAddressReverseRequest();

            searchAddressReverseRequest.Query = $"{latitude},{longitude}";

            logger.LogInformation("Getting address for searchAddressReverseRequest: {0}", JsonSerializer.Serialize(searchAddressReverseRequest));

            var response = await azureMaps.GetSearchAddressReverse(searchAddressReverseRequest).ConfigureAwait(false);

            logger.LogInformation("Response from getting address for searcAddressReverseRequest: {0}", JsonSerializer.Serialize(response));

            if (response.HttpResponseCode != (int)HttpStatusCode.OK && response.HttpResponseCode != 0)
            {
                throw new Exception($"Error getting address: {response}");
            }

            var address = new Address
            {
                StreetAddress = response?.Result?.Addresses[0].Address.StreetNameAndNumber,
                City          = response?.Result?.Addresses[0].Address.Municipality,
                Country       = response?.Result?.Addresses[0].Address.Country,
                Region        = response?.Result?.Addresses[0].Address.CountrySubdivisionName,
                PostalCode    = response?.Result?.Addresses[0].Address.PostalCode,
                County        = response?.Result?.Addresses[0].Address.CountrySecondarySubdivision,
            };

            return(address);
        }