Exemple #1
0
        public async Task PermissaoGPSAsync()
        {
            try
            {
                var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);

                //Verifica se a permissao é diferente de aceita
                if (status != PermissionStatus.Granted)
                {
                    if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
                    {
                        await DisplayAlert("Need location", "Gunna need that location", "OK");
                    }

                    var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);

                    //Best practice to always check that the key exists
                    if (results.ContainsKey(Permission.Location))
                    {
                        status = results[Permission.Location];
                    }
                }

                //Permissao for aceita
                if (status == PermissionStatus.Granted)
                {
                    MapContainer.Children.RemoveAt(0);
                    GPS gps = new GPS();
                    if (gps.IsLocationAvailable())
                    {
                        Plugin.Geolocator.Abstractions.Position pos = gps.GetCurrentPosition().GetAwaiter().GetResult();

                        mapa = new Map(MapSpan.FromCenterAndRadius(new Position(pos.Latitude, pos.Longitude), Distance.FromMeters(100)));
                        MapContainer.Children.Add(mapa);

                        var propostas = await service.GetPropostaAsync();

                        foreach (var item in propostas)
                        {
                            mapa.Pins.Add(pin = new Pin
                            {
                                Position = new Position(Convert.ToDouble(item.Latitude), Convert.ToDouble(item.Longitude)),
                                Label    = "ID da Vaga:" + item.IDProposta + "," + "\nID da Empresa:" + item.IDEmpresa + "," + "\nDescricao da Vaga:" + item.Descricao + "," +
                                           "\nTipo de Contrato:" + item.TipoContrato + "," + "\nCidade:" + item.Cidade,
                                Address = item.Latitude + "," + item.Longitude,
                                Type    = PinType.Place,
                            });

                            pin.InfoWindowClicked += async(s, args) =>
                            {
                                try
                                {
                                    _descricao    = ((Pin)s).Label;
                                    posicao       = ((Pin)s).Address;
                                    _posicao      = posicao.Split(',');
                                    strnomes      = _descricao.Split(',');
                                    _idprop       = strnomes[0].Split(':');
                                    _idemp        = strnomes[1].Split(':');
                                    _desc         = strnomes[2].Split(':');
                                    _tipocontrato = strnomes[3].Split(':');
                                    _cidade       = strnomes[4].Split(':');
                                    await DisplayAlert("Informações da Vaga", $"{_descricao}", "OK");

                                    _IdProposta = _idprop[1];
                                }
                                catch (Exception)
                                {
                                    throw;
                                }
                            };
                            pin.Clicked += Pin_Clicked;
                        }
                    }
                }
                //Não aceitaa
                else if (status != PermissionStatus.Unknown)
                {
                    await DisplayAlert("Location Denied", "Can not continue, try again.", "OK");
                }
            }
            catch (Exception)
            {
            }
        }