反向GeoCoding Android问题

时间:2018-10-16 04:01:05

标签: java android geocoding

我无法将坐标转换为实际地址。

我有两个变量可以拉出坐标,但是当我调整代码时会遇到很多错误。第一个错误是“未报告的异常IOException;必须被捕获或声明为抛出”,然后添加try catch,然后弹出另一个错误,“您的地址可能尚未初始化。

我只是想获取地址,街道和城市,所以可以将其附加到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);
            }

        }

谢谢!

2 个答案:

答案 0 :(得分:0)

我为我使用了此代码及其工作。...

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();
}

这是我的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;
        }
    }
}

答案 1 :(得分:0)

您需要为yourAddress初始化一些

List<Address> yourAddresses = new ArrayList();
相关问题