Exemple #1
0
        public async Task <CreateBioLogResponse> Handle(CreateBioLogRequest request, CancellationToken cancellationToken)
        {
            var employee = await dbContext.Employees.FirstOrDefaultAsync(i => i.EmployeeNumber.ToLower() == request.EmployeeNumber.ToLower());

            if (employee == null)
            {
                throw new NotFoundException(nameof(domain.Employee), request.EmployeeNumber);
            }


            var geoCoder = new Geocoder("271460ae98bf4e2baae91c779d36992b");
            var result   = await geoCoder.ReverseGeocodeAsync(request.Lat, request.Long);

            var bio = new BioLog
            {
                Lat        = request.Lat,
                Long       = request.Long,
                EmployeeID = employee.ID,
                LogType    = request.LogType,
                Time       = DateTimeOffset.Now,
                Location   = result.Results.Length != 0 ? result.Results[0].Formatted : "n/a",
            };

            Blame(bio, employee.CompanyEmail, employee.CompanyEmail);

            dbContext.RawLogs.Add(bio);

            await dbContext.SaveChangesAsync(cancellationToken);

            return(new CreateBioLogResponse(nameof(BioLog), bio.ID));
        }
Exemple #2
0
        public async Task <RuntimeResult> GetWeatherForLocationAsync([Summary("User")] SocketUser user = null)
        {
            var targetUser = user ?? Context.User;
            var record     = UserRepository.GetCoordinates(targetUser);

            if (record == null)
            {
                return(CommandRuntimeResult.FromError(
                           "Location is not set yet! Please set the location first!"));
            }
            var geocodeResults = await Geocoder.ReverseGeocodeAsync(record.Latitude, record.Longitude).ConfigureAwait(false);

            var geocode = geocodeResults.FirstOrDefault();

            if (geocode == null)
            {
                return(CommandRuntimeResult.FromError(
                           "I could not find the set location! Try setting another location."));
            }
            var forecast = await DarkSkyService.GetForecast(
                geocode.Coordinates.Latitude,
                geocode.Coordinates.Longitude,
                _darkSkyParams
                ).ConfigureAwait(false);

            var embeds = await Weather.GetWeatherEmbedsAsync(forecast, geocode).ConfigureAwait(false);

            await ReplyAsync("", embed : embeds.WeatherResults.FirstOrDefault().Build()).ConfigureAwait(false);

            foreach (var alert in embeds.Alerts)
            {
                await ReplyAsync("", embed : alert.Build()).ConfigureAwait(false);
            }
            return(CommandRuntimeResult.FromSuccess());
        }
Exemple #3
0
        /// <summary>
        /// Test all the methods of the coder.
        /// </summary>
        /// <returns>Task to await.</returns>
        public static async Task AllAsync()
        {
            string apiKey = string.Empty;

            Geocoder coder = new Geocoder(apiKey);

            string address = "1600 Amphitheatre Parkway";

            Console.WriteLine($"Geocode address:'{address}' with language and region");
            PrintResponse(await coder.GeocodeAsync(address, language: "en", region: "us"));

            Console.WriteLine(string.Empty);
            Console.WriteLine($"Geocode address:'{address}' with bounds filter");

            // Antioquia bounds 5.418365,-77.135572|8.8814071,-73.871107
            Bounds boundFilter = new Bounds()
            {
                Southwest = new Southwest()
                {
                    Lat = 5.418365,
                    Lng = -77.135572
                },
                Northeast = new Northeast()
                {
                    Lat = 8.8814071,
                    Lng = -73.871107
                }
            };

            PrintResponse(await coder.GeocodeAsync(address, language: "en", boundsBias: boundFilter));

            Console.WriteLine(string.Empty);
            Console.WriteLine($"Geocode address:'{address}' with component filter country");
            PrintResponse(await coder.GeocodeAsync(address, component: new ComponentFilter()
            {
                Country = "us"
            }));

            Console.WriteLine(string.Empty);
            Console.WriteLine($"Geocode address:'{address}' with component filter country and administrative area");
            PrintResponse(await coder.GeocodeAsync(address, component: new ComponentFilter()
            {
                Country = "us", AdministravieArea = "California"
            }));

            Console.WriteLine(string.Empty);
            Console.WriteLine($"Geocode address:'{address}' with component filter country and administrative area, language and department bounds.");
            PrintResponse(await coder.GeocodeAsync(address, component: new ComponentFilter()
            {
                Country = "us", AdministravieArea = "California"
            }, language: "en", boundsBias: boundFilter));

            Console.WriteLine(string.Empty);
            float latitude  = 37.4224764f;
            float longitude = -122.0842499f;

            Console.WriteLine($"ReverseGeocode lat:{latitude};long:{longitude}");
            PrintResponse(await coder.ReverseGeocodeAsync(latitude, longitude));
        }
Exemple #4
0
        private async void InitializeLocation()
        {
            try
            {
                // Check if the geolocator is available
                var geolocator = CrossGeolocator.Current;
                if (geolocator != null && geolocator.IsGeolocationEnabled && geolocator.IsGeolocationAvailable)
                {
                    // Start a timer that queues the position request timeout
                    var timeoutTask = Task.Delay(LocationTimeoutMilliseconds);
                    // Start a task to get the current position
                    var positionTask = geolocator.GetPositionAsync();
                    // Wait for the position or timeout task to complete
                    var positionOrTimeoutTask = await Task.WhenAny(positionTask, timeoutTask);

                    // If it was the position task that completed first...
                    if (positionOrTimeoutTask != timeoutTask)
                    {
                        // Update the survey GPS location
                        var position = await positionTask;
                        _response.Item.GPSLocation.Lat      = position.Latitude;
                        _response.Item.GPSLocation.Lon      = position.Longitude;
                        _response.Item.GPSLocation.Accuracy = (float)position.Accuracy;
                        // Try to reverse geocode the position to a nearby address
                        var address = await Geocoder.ReverseGeocodeAsync(position.Latitude, position.Longitude);

                        if (address != null)
                        {
                            Street  = address.AddressLine;
                            City    = address.Locality;
                            State   = address.AdminDistrict;
                            ZipCode = address.PostalCode;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DependencyService.Get <IMetricsManagerService>().TrackException("ReverseGeocodeFailed", ex);
            }
            finally
            {
                Position = PrettyPrintPosition();
                IsBusy   = false;
            }
        }
        public async Task <OpenCageResponseReverse> GetAddressFromCoordinate([FromQuery] double latitude, [FromQuery] double longitude, [FromQuery] string openCageApiKey)
        {
            var gc            = new Geocoder(openCageApiKey);
            var reverseResult = await gc.ReverseGeocodeAsync(latitude, longitude);

            var locationDictionary = reverseResult.Results.FirstOrDefault()?.ComponentsDictionary;

            //var foo = gc.Geocode("asdf");
            //var faa = foo.Results.FirstOrDefault()?.ComponentsDictionary;

            var openCageResponse = new OpenCageResponseReverse
            {
                StatusCode    = reverseResult.Status.Code,
                StatusMessage = reverseResult.Status.Message
            };

            openCageResponse.TryParseDictionaryResponse(locationDictionary);
            return(openCageResponse);
        }