Tengo problemas para convertir las coordenadas en una dirección real.

Tengo dos variables que extraen coordenadas, pero obtengo muchos errores cuando modifico el código. El primer error es "excepción IOException no reportada; debe ser detectada o declarada para ser lanzada" y luego agrego la captura de prueba y luego aparece otro error, "yourAddresses podría no haberse inicializado.

Solo intento obtener la dirección, la calle y la ciudad para poder agregarlas a textView.

        @Override
        public void onLocationChanged(Location location) 
        {


            double latitude = location.getLongitude();
            double longitude = location.getLatitude();

            //t.append("\n " + location.getLongitude() + " " + location.getLatitude());

            Geocoder geocoder;
            List<Address> yourAddresses;
            geocoder = new Geocoder(context, Locale.getDefault());



              yourAddresses = geocoder.getFromLocation(latitude, longitude, 1);

            if (yourAddresses.size() > 0) {
                String yourAddress = yourAddresses.get(0).getAddressLine(0);
                String yourCity = yourAddresses.get(0).getAddressLine(1);
                String yourCountry = yourAddresses.get(0).getAddressLine(2);
            }

        }

¡Gracias!

1
klykins 16 oct. 2018 a las 07:01

2 respuestas

La mejor respuesta

Usé este código y funciona para mí ...

public void getAddressFromLocation(final double latitude, final double longitude,
                                          final Context context, final Handler handler)
{
    Thread thread = new Thread()
    {
        @Override
        public void run()
        {
            Geocoder geocoder = new Geocoder(context, Locale.getDefault());
            String result = null;
            Address address = null;

            try
            {
                List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1);

                if (addressList != null && addressList.size() > 0)
                {
                    address = addressList.get(0);
                }
            }
            catch (Exception e)
            {
                Log.e(TAG, "getAddressFromLocation:run: exception while getting address from location");
                e.printStackTrace();
            }
            finally
            {
                Message message = Message.obtain();
                message.setTarget(handler);

                if (address != null)
                {
                    message.what = 1;
                    Bundle bundle = new Bundle();

                    bundle.putString("thoroughFare", address.getThoroughfare());
                    bundle.putString("subThoroughFare", address.getSubThoroughfare());
                    bundle.putString("city", address.getLocality());
                    bundle.putString("state", address.getAdminArea());
                    bundle.putString("country", address.getCountryName());
                    bundle.putString("postalCode", address.getPostalCode());
                    bundle.putString("subAdminArea", address.getSubAdminArea());
                    bundle.putString("subLocality", address.getSubLocality());
                    message.setData(bundle);
                }
                else
                {
                    message.what = 1;
                    Bundle bundle = new Bundle();

                    result = "Latitude: " + latitude + "Longitude: " + longitude +
                            "\n Unable to get address for this location.";

                    bundle.putString("address", result);
                    message.setData(bundle);
                }

                message.sendToTarget();
            }
        }
    };

    thread.start();
}

Esta es mi clase GeoCoderHandler ...

private class GeoCoderHandler extends Handler
{
    @Override
    public void handleMessage(Message msg)
    {
        switch (msg.what)
        {
            case 1:

                String address = "";
                Bundle bundle = msg.getData();

                if (bundle.getString("subThoroughFare") != null)
                {
                    if (!bundle.getString("subThoroughFare").equalsIgnoreCase("null"))
                    {
                        address = bundle.getString("subThoroughFare") + ", " +
                                bundle.getString("thoroughFare");
                    }
                }
                else
                {
                    address = bundle.getString("thoroughFare");
                }

                tvAddress1.setText("");
                tvAddress1.setText(address);

                tvAddress2.setText("");
                tvAddress2.setText(bundle.getString("subLocality"));

                tvAddress3.setText("");
                tvAddress3.setText(bundle.getString("subAdminArea"));

                edtPinCode.setText("");
                edtPinCode.setText(bundle.getString("postalCode"));

                tvCity.setText("");
                tvCity.setText(bundle.getString("city"));

                tvState.setText("");
                tvState.setText(bundle.getString("state"));

                tvCountry.setText("");
                tvCountry.setText(bundle.getString("country"));

                break;

            default:

                tvAddress1.setText(getResources().getString(R.string.address_not_found));
                tvAddress2.setText("");
                tvAddress3.setText("");
                edtPinCode.setText("");
                tvCity.setText("");
                tvState.setText("");
                tvCountry.setText("");

                break;
        }
    }
}
0
Amit Jangid 16 oct. 2018 a las 04:11

Su necesidad inicializada para yourAddress

List<Address> yourAddresses = new ArrayList();
0
Ali bana 16 oct. 2018 a las 04:42