无法从坐标中检索街道地址

时间:2015-12-20 09:09:27

标签: android

我试图通过提供经度和纬度坐标来获取完整地址。现在我成功地获得了正确的坐标 这就是我得到的:

my coordinates﹕ 32.6653854,35.1051237

现在当我使用getCompleteAddressString() 我总是得到例外:

Current location address﹕ Can not get Address!

这是我的getCompleteAddressString函数:

 public String getCompleteAddressString(double LATITUDE, double LONGITUDE, Context ctx) {
    String strAdd = "";
    Geocoder geocoder = new Geocoder(ctx, 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("\n");
            }
            strAdd = strReturnedAddress.toString();
            Log.w("My Current location address", "" + strReturnedAddress.toString());
        } else {
            Log.w("My Current location address", "No Address returned!");
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.w("My Current location address", "Can not get Address!");
    }
    return strAdd;
}

这是我尝试运行它的类:

@Override
public void updateUI() {
    Log.d("my location","updateUI");
    Log.d("get coordinates","my location");
    String realAddress = myLocManager.getCompleteAddressString(myLocManager.getmLastLocation().getLatitude(),myLocManager.getmLastLocation().getLatitude(),this.getActivity().getApplicationContext());
    Log.d("my coordinates", String.valueOf(myLocManager.getmLastLocation().getLatitude()+","+myLocManager.getmLastLocation().getLongitude()));
    Log.d("my real address",realAddress);
    if(realAddress != null)
        streetAddress.setText(realAddress);
}

我尝试使用getActivity()方法运行updateUI但仍然遇到了同样的错误。 当我在其他应用程序中使用LocationManager时,它运行良好。

1 个答案:

答案 0 :(得分:0)

我的错误是用wrond参数调用方法:

getmLastLocation().getLatitude(),myLocManager.getmLastLocation().getLatitude()

我通过Latitude param两次