Example #1
0
        public static async Task <GoogleGeoCodeResponse> PegaEnderecoPorCEP(string cep)
        {
            try
            {
                //limpa o cep removendo tudo que não for númerico
                cep = Regex.Replace(cep, "[^0-9]", "");

                string sCep = "";

                if (cep.IndexOf("-") < 0)
                {
                    sCep = string.Format("{0}-{1}", cep.Substring(0, 5), cep.Substring(5, 3));
                }

                //chama a api rest
                var result = await new RestService(GOOGLE_API_RESOURCE, GOOGLE_API_END_POINT, GOOGLE_API_BASE_URI, sCep).PegaEnderecoGeoReversoPorCEP <GoogleGeoCodeResponse> ();

                //testa o retorno
                if (result != null)
                {
                    //convert o retorno para o tipo esperado
                    GoogleGeoCodeResponse addresses = (GoogleGeoCodeResponse)result;
                    return(addresses);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("{0} : {1}", "PegaEnderecoPorCEP", ex.Message));
            }

            return(null);
        }
Example #2
0
        protected void AtribuiEndereco(GoogleGeoCodeResponse enderecoRetorno)
        {
            try
            {
                if (!AnalisaStatusAPIGoogle(enderecoRetorno.status))
                {
                    Application.Current.MainPage = new NavigationPage(new StatusConexao());
                    return;
                }
                else
                {
                    //preenche o objeto ocorrência de acordo com o endereço encontrado
                    Sessao.OcorrenciaAtiva.Endereco = enderecoRetorno.results [0].address_components [1].long_name;
                    Sessao.OcorrenciaAtiva.Estado   = enderecoRetorno.results [0].address_components [5].short_name;

                    if (!string.IsNullOrEmpty(entCEP.Text))
                    {
                        Sessao.OcorrenciaAtiva.Cep = entCEP.Text;
                    }
                    else
                    {
                        Sessao.OcorrenciaAtiva.Cep = enderecoRetorno.results [0].address_components [7].long_name;
                    }

                    Sessao.OcorrenciaAtiva.Cidade = enderecoRetorno.results [0].address_components [3].long_name;
                    Sessao.OcorrenciaAtiva.Bairro = enderecoRetorno.results [0].address_components [2].long_name;
                    Sessao.OcorrenciaAtiva.Data   = DateTime.UtcNow;
                    Sessao.OcorrenciaAtiva.LocalizacaoReconhecida = true;

                    Sessao.OcorrenciaAtiva.EnderecoFormatado = string.Format("{0}, {1} - {2}/{3}",
                                                                             enderecoRetorno.results [0].address_components [1].long_name,
                                                                             enderecoRetorno.results [0].address_components [2].long_name,
                                                                             enderecoRetorno.results [0].address_components [3].long_name,
                                                                             enderecoRetorno.results [0].address_components [5].short_name);


                    lblEndereco.Text = Sessao.OcorrenciaAtiva.Endereco;
                    lblCidade.Text   = Sessao.OcorrenciaAtiva.Cidade;
                    lblCEP.Text      = Sessao.OcorrenciaAtiva.Cep;
                }
            }
            catch (Exception ex)
            {
                DisplayLog(string.Format("{0} : {1} - {2}", enderecoRetorno.status, "AtribuirEndereco: ", ex.Message));
            }
        }
Example #3
0
        public static async Task <GoogleGeoCodeResponse> PegaEnderecoPorCoordenadas(string latitude, string longitude, bool desabilitarfiltros = false)
        {
            //chama a api rest
            var result = await new RestService(
                GOOGLE_API_RESOURCE,
                GOOGLE_API_END_POINT,
                GOOGLE_API_BASE_URI,
                latitude,
                longitude,
                desabilitarfiltros).PegaEnderecoGeoReversoPorCoordenada <GoogleGeoCodeResponse>();

            //testa o retorno
            if (result != null)
            {
                GoogleGeoCodeResponse address = (GoogleGeoCodeResponse)result;
                return(address);
            }

            return(null);
        }
Example #4
0
        protected async Task ObtemEnderecoPorCoordenada()
        {
            try
            {
                IsBusy = true;

                GoogleGeoCodeResponse enderecoRetorno = await Sessao.PegaEnderecoPorCoordenadas(Sessao.OcorrenciaAtiva.Latitude.ToString(), Sessao.OcorrenciaAtiva.Longitude.ToString());

                if (enderecoRetorno != null)
                {
                    AtribuiEndereco(enderecoRetorno);
                }
            }
            catch (Exception ex)
            {
                DisplayLog(string.Format("{0} : {1}", "ObtemEnderecoPorCoordenada", ex.Message));
            }finally{
                IsBusy = false;
            }
        }
Example #5
0
        protected async Task ObtemEnderecoPorCEP(string zipcode = "")
        {
            IsBusy = true;

            try
            {
                if (!string.IsNullOrEmpty(zipcode))
                {
                    entCEP.Text = zipcode;
                }

                if (!string.IsNullOrEmpty(entCEP.Text.ToString()))
                {
                    string cep = Regex.Replace(entCEP.Text, "[^0-9]", "");

                    if (!string.IsNullOrEmpty(cep) || cep.Length >= 7)
                    {
                        //carrega o endereço pelo cep?
                        GoogleGeoCodeResponse enderecoRetorno = await Sessao.PegaEnderecoPorCEP(cep);


                        //ao escolher a lista temos que atribuir a latitude e longitude ao objeto de ocorrencia

                        if (enderecoRetorno != null)
                        {
                            Sessao.GeoReturn = enderecoRetorno;

                            string lat = enderecoRetorno.results[0].geometry.location.lat;
                            string lng = enderecoRetorno.results[0].geometry.location.lng;

                            if ((string.IsNullOrEmpty(lat) || lat.Equals("0")) || (string.IsNullOrEmpty(lng) || lng.Equals("0")))
                            {
                                await DisplayAlert("TáVazando!", "Coordenadas não encontradas. Informe um CEP inválido.", "Ok");

                                return;
                            }

                            //faz uma nova chamada a API para pegar os endereços nesse cep
                            GoogleGeoCodeResponse endereco = await Sessao.PegaEnderecoPorCoordenadas(lat, lng);

                            if (endereco != null)
                            {
                                //preenche o objeto ocorrência de acordo com o endereço encontrado
                                AtribuiEndereco(endereco);

//									double latitude = Sessao.ConvertCoordenate(enderecoRetorno.results[0].geometry.location.lat);//enderecoRetorno.results[0].geometry.location.lat.IndexOf(",") >-1 ? Convert.ToDouble(enderecoRetorno.results[0].geometry.location.lat.Replace(",",".")): Convert.ToDouble(enderecoRetorno.results[0].geometry.location.lat);
//									double longitude = Sessao.ConvertCoordenate(enderecoRetorno.results[0].geometry.location.lng);//enderecoRetorno.results[0].geometry.location.lng.IndexOf(",") >-1 ? Convert.ToDouble(enderecoRetorno.results[0].geometry.location.lng.Replace(",",".")): Convert.ToDouble(enderecoRetorno.results[0].geometry.location.lng);

                                Sessao.OcorrenciaAtiva.Latitude     = Sessao.ConvertCoordenate(enderecoRetorno.results[0].geometry.location.lat);                             // latitude;
                                Sessao.OcorrenciaAtiva.Longitude    = Sessao.ConvertCoordenate(enderecoRetorno.results[0].geometry.location.lng);                             //longitude;
                                Sessao.OcorrenciaAtiva.CepCorrigido = cep;

                                CriarMapa();
                            }
                            else
                            {
                                await DisplayAlert("TáVazando!", "Nenhum endereço encontrado", "Ok");
                            }
                        }
                        else
                        {
                            await DisplayAlert("TáVazando!", "Nenhum endereço encontrado", "Ok");
                        }
                    }
                    else
                    {
                        await DisplayAlert("TáVazando!", "Informe um CEP para pesquisar", "Ok");
                    }
                }
            }
            catch (Exception ex)
            {
                DisplayLog(string.Format("{0} : {1}", "ObtemEnderecoPorCEP", ex.Message));
            }
            finally
            {
                IsBusy = false;
            }
        }