如何从坐标获取街道名称?

时间:2015-01-10 12:05:32

标签: android gps street-address

我有两个独立的EditText的经度和纬度我希望当我按下按钮时,街道名称出现在另一个EditText中。

我尝试使用Public Address getAddressForLocation方法,但我没有让它工作..

代码

public Address getAddressForLocation(Context context, Location location) throws IOException {

        if (location == null) {
            return null;
        }
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        int maxResults = 1;

        Geocoder gc = new Geocoder(context, Locale.getDefault());
        List<Address> addresses = gc.getFromLocation(latitude, longitude, maxResults);

        for (int i = 0; i < addresses.getMaxAddressLineIndex(); i++) {
            Log.d("=Adress=",addresses.getAddressLine(i));
        }
    }

如何从坐标中获取街道名称?

更新(解决方案)

Geocoder geocoder = new Geocoder(this, Locale.getDefault());

            try {
                List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

                if (addresses != null) {
                    Address returnedAddress = addresses.get(0);
                    StringBuilder strReturnedAddress = new StringBuilder();
                    for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                        strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("");
                    }
                    et_lugar.setText(strReturnedAddress.toString());
                }
                else {
                    et_lugar.setText("No Address returned!");
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                et_lugar.setText("Canont get Address!");
            }

由于

3 个答案:

答案 0 :(得分:3)

docs提及getThoroughfare()方法,该方法可能为空。我先尝试一下。如果为null,那么我会尝试从getAddressLine()获取有用的内容。您可能无法获得所有案例的街道名称。

答案 1 :(得分:3)

<强>解

Geocoder geocoder = new Geocoder(this, Locale.getDefault());

                try {
                    List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

                    if (addresses != null) {
                        Address returnedAddress = addresses.get(0);
                        StringBuilder strReturnedAddress = new StringBuilder();
                        for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                            strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("");
                        }
                        et_lugar.setText(strReturnedAddress.toString());
                    }
                    else {
                        et_lugar.setText("No Address returned!");
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    et_lugar.setText("Canont get Address!");
                }

答案 2 :(得分:0)

首先,您的手机是否支持位置?你需要发布错误,所以我们知道什么是真正的问题

假设您的手机支持位置,请尝试使用getAdressLine():

    List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
    Log.d("=Adress=",addresses.get(0).getAddressLine(0));

这里的catch是从具有地址类结构的列表中得到你想要的东西