简体   繁体   English

无法从Android中的经度和纬度获取地址

[英]Can't get address from longitude and latitude in android

I am trying to get my address from longitude and latitude , but when I am trying to do so , it doesn't work . 我试图从经度和纬度获取地址,但是当我这样做时,它无效。 It says W/System.err﹕ at teamtreehouse.com.stromy.MainActivity.getCompleteAddressString(MainActivity.java:163) in logcat 它在logcat中的teamtreehouse.com.stromy.MainActivity.getCompleteAddressString(MainActivity.java:163)上显示W / System.err:

Here is the method I have tried 这是我尝试过的方法

private   String getCompleteAddressString() {
    String strAdd = "";
    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("\n");
            }
            strAdd = strReturnedAddress.toString();
            //     Log.w("My Current loction address", "" + strReturnedAddress.toString());

        } else {
            //    Log.w("My Current loction address", "No Address returned!");
        }
    } catch (Exception e) {
        e.printStackTrace();
        //  Log.w("My Current loction address", "Canont get Address!");
    }
    return strAdd;
}

And I call it like 我这样称呼它

public void updateDisplay() {
    mLocation.setText(getCompleteAddressString());
}

In getCompleteAddressString() method , I have used longitude and latitude , which I have already got from a another method and it is working fine . 在getCompleteAddressString()方法中,我使用了经度和纬度,这是我已经从另一个方法获得的,并且可以正常工作。 Both method are running withing main activity . 两种方法都以main活动运行。 All other things are working fine except this , how can I solve it ? 除此之外,其他所有东西都工作正常,我该如何解决? what are reason of this error ? 此错误的原因是什么?

Try like this way: 像这样尝试:

        Geocoder gcd = new Geocoder(this, Locale.getDefault());
        List<Address> addresses = null;
        Address addr = null;
        try {
            addresses = gcd.getFromLocation(latitude, longitude, 1);
            if (addresses != null && addresses.size() > 0) {
                addr = addresses.get(0);
                String info = "Address is:  ";
                info += addr.getMaxAddressLineIndex() > 0 ? addr
                        .getAddressLine(0) : "";
                info = info + ", " + addr.getLocality() + ", "
                        + addr.getCountryName();
                Toast.makeText(getApplicationContext(), info,
                        Toast.LENGTH_LONG).show();
            } else
                Toast.makeText(getApplicationContext(),
                        "Address not found", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Address not found",
                    Toast.LENGTH_LONG).show();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM