Exemplo n.º 1
0
        public async void RecupererPosition()
        {
            Position position = null;

            try
            {
                IGeolocator localisation = CrossGeolocator.Current;
                localisation.DesiredAccuracy = 100;
                position = await localisation.GetLastKnownLocationAsync();

                if (position != null)
                {
                    Latitude  = position.Latitude;
                    Longitude = position.Longitude;
                    IEnumerable <Address> addresses = await localisation.GetAddressesForPositionAsync(position, null);

                    Address address = addresses.FirstOrDefault();
                    Adresse = address.Thoroughfare + ", " + address.PostalCode + " " + address.Locality;
                }
                else
                {
                    if (!localisation.IsGeolocationAvailable || !localisation.IsGeolocationEnabled)
                    {
                        Console.WriteLine("Erreur !");
                        return;
                    }
                    else
                    {
                        position = await localisation.GetPositionAsync(TimeSpan.FromSeconds(20), null, true);

                        if (position != null)
                        {
                            Latitude  = position.Latitude;
                            Longitude = position.Longitude;
                            IEnumerable <Address> addresses = await localisation.GetAddressesForPositionAsync(position, null);

                            Address address = addresses.FirstOrDefault();
                            Adresse = address.Thoroughfare + ", " + address.PostalCode + " " + address.Locality;
                        }
                        else
                        {
                            Latitude  = 0;
                            Longitude = 0;
                            Adresse   = "Inconnu";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Impossible de récupérer la location: " + ex);
            }
        }
Exemplo n.º 2
0
        public async Task <Address> GetAddressGeoLocation()
        {
            IGeolocator locator = CrossGeolocator.Current;

            locator.DesiredAccuracy = 100;

            Position position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10));

            var posAddress = await locator.GetAddressesForPositionAsync(position, null);

            Address address = posAddress.FirstOrDefault();

            return(address);
        }
Exemplo n.º 3
0
        private async Task <Tuple <WeatherApiResonseData, OneCallWeatherAPIResponseData> > GetLocation()
        {
            IGeolocator locator = CrossGeolocator.Current;

            locator.DesiredAccuracy = 50;

            Position positions = await locator.GetPositionAsync(timeout : new TimeSpan(10000));

            IEnumerable <Address> address = await locator.GetAddressesForPositionAsync(positions);

            WeatherApiResonseData weather = await OpenWeatherService.GetCurrentWeatherByCityNameAsync(address.FirstOrDefault().Locality);

            OneCallWeatherAPIResponseData weatherDetails = await OpenWeatherService.GetOneCallAPIRequestAsync(weather.coord.lat, weather.coord.lon);

            return(Tuple.Create(weather, weatherDetails));
        }
Exemplo n.º 4
0
        private async Task <string> GetPositionAddress(IGeolocator locator, Position position)
        {
            string address = string.Empty;

            try
            {
                IEnumerable <Address> addresses = await locator.GetAddressesForPositionAsync(position);

                var addressItem = addresses.FirstOrDefault();
                if (addressItem != null)
                {
                    address = $"{addressItem.SubThoroughfare}, {addressItem.Thoroughfare}, {addressItem.Locality}, {addressItem.CountryName}";
                    if (!string.IsNullOrEmpty(addressItem.SubLocality))
                    {
                        _pointNameFromAddress = $"{addressItem.SubLocality}";
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(addressItem.Locality))
                        {
                            _pointNameFromAddress = $"{addressItem.CountryName},{addressItem.Locality}";
                        }
                        else
                        {
                            _pointNameFromAddress = $"{addressItem.CountryName},{addressItem.SubAdminArea}";
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                var properties = new Dictionary <string, string> {
                    { "Screen", "RoutePoint" }, { "Action", "GetPositionAddress" }
                };
                Crashes.TrackError(exception, properties);
            }
            return(address);
        }
Exemplo n.º 5
0
        public async void PrintPosition(Position position, string title)
        {
            double lat       = position.Latitude;
            double lon       = position.Longitude;
            var    timeStamp = position.Timestamp;
            string time      = timeStamp.DateTime.ToString();

            double alt = position.Altitude;

            double head  = position.Heading;
            double speed = position.Speed;


            IEnumerable <Address> addresses = await geolocator.GetAddressesForPositionAsync(position);

            Address address = new Address();

            if (addresses != null && addresses.GetEnumerator().MoveNext())
            {
                address = addresses.GetEnumerator().Current;
            }

            foreach (Address a in addresses)
            {
                address = a;
            }

            string strAddress = String.Format("{0} {1}, {2}, {3} {4}",
                                              address.SubThoroughfare, address.Thoroughfare, address.Locality, address.AdminArea, address.PostalCode);

            Console.WriteLine(title + "\n" + time + "\n" +
                              String.Format("Lat: {0}, Lon: {1}, Altitude: {2}, Heading: {3}, Speed: {4}\n" +
                                            "Address: {5}\n" +
                                            "Address Lat: {6}, Address Lon: {7}",
                                            lat, lon, alt, head, speed, strAddress, address.Latitude, address.Longitude));
        }
Exemplo n.º 6
0
        private async void ConvertLongLatToAddressAsync(Position positionUser, IGeolocator locator)
        {
            var addresses = await locator.GetAddressesForPositionAsync(positionUser);


            // Array ListAdd;
            if (addresses.Count() > 0)
            {
                // ObservableCollection<ListViewAddressStruct> ListViewAddressStruct = new ObservableCollection<ListViewAddressStruct>();
                List <ListViewAddressStruct> ListViewAddressStruct = new List <ListViewAddressStruct>();
                //  List<ListViewAddressStruct> ListAdd = new List<ListViewAddressStruct>();
                //  int[] ListAdd = new int[addresses.Count()];
                // var ListAdd2 = new List<string>();

                // Console.WriteLine("addresses.Count()=" + addresses.Count());
                int I = 0;
                foreach (var address in addresses)
                {
                    I = I + 1;

                    if (address == null)
                    {
                        await DisplayAlert("Alert", "No address found. Please enter your address details manually", "OK");

                        // applicationSpinner.IsVisible = false;
                    }
                    else
                    {
                        ListViewAddressStruct item = new ListViewAddressStruct();

                        //  Console.WriteLine(" address.Thoroughfare:" + address.Thoroughfare + " address.Locality:" + address.Locality + " address.SubThoroughfare:" + address.SubThoroughfare + " address.AdminArea:" + address.AdminArea + " address.CountryCode:" + address.CountryCode + " address.CountryName:" + address.CountryName + " address.FeatureName:" + address.FeatureName + " address.Locality:" + address.Locality + " address.Latitude:" + address.Latitude + " address.Longitude:" + address.Longitude + " address.PostalCode:" + address.PostalCode + " address.SubAdminArea:" + address.SubAdminArea + " address.SubLocality:" + address.SubLocality + " address.SubThoroughfare:" + address.SubThoroughfare + " address.Thoroughfare:" + address.Thoroughfare );
                        //  RueXAML.Text = address.Thoroughfare;

                        //  NumeroXAML.Text = address.Locality;
                        //  CodePostalXAML.Text = address.SubThoroughfare;
                        item.Id         = I;
                        item.CodePostal = address.PostalCode;
                        item.Latitude   = address.Latitude.ToString();
                        item.Longitude  = address.Longitude.ToString();
                        item.Numero     = address.SubThoroughfare;
                        item.Pays       = address.CountryName;
                        item.Rue        = address.Thoroughfare;
                        item.Ville      = address.Locality;


                        ListViewAddressStruct.Add(item);
                    }
                }



                ListAddXAML.ItemsSource = ListViewAddressStruct;


                ListAddXAML.ItemSelected += (object sender, SelectedItemChangedEventArgs e) => {
                    AddresseSelect(sender, e);
                };

                ShowPopUp("waiting", false);
                ShowPopUp("address", true);
            }
            else
            {
                await DisplayAlert("Alert", "No address found. Please enter your address details manually", "OK");

                // applicationSpinner.IsVisible = false;
            }
        }