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

How to get street name from coordinates?

我将经度和纬度分为两个单独的 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!");
            }

谢谢

docs提到了getThoroughfare()方法,可能为空。我会先试试看。如果为 null,那么我会尝试从 getAddressLine() 中获取一些有用的东西。您可能无法获得所有情况下的街道名称。

首先,您的 phone 支持定位吗?您还需要 post 错误,以便我们知道真正的问题是什么

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

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

这里的问题是从具有地址 class 结构的列表中获取您想要的内容

解决方案

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!");
                }