1

我使用谷歌地图的反向地理编码来获取地址,第一次工作时,完全输入地址,但现在它只显示纬度和经度,没有名字,在任何地址。

有人能帮我吗?

我的代码:

private class GetAddressTask extends AsyncTask<Location, Void, String> {

    @Override
    protected String doInBackground(Location... params) {
        Geocoder geocoder = new Geocoder(MainActivity.this);
        Location loc = params[0];
        List<Address> addresses = null;

        try {
            addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
        } catch (IOException e) {
            return null;
        }

        if( addresses != null && addresses.size() > 0 ) {
            Address addr = addresses.get(0);
            String addressText = addr.getAddressLine(0);
            updateLastStreet(addressText, MainActivity.this);
            return addressText;
        }

        return null;
    }
}
4

1 回答 1

1

您是否缺少 Android.Manifest 文件中的权限,然后添加此

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

并尝试这种方法:

public List<Address> getAddress(LatLng point) {
    try {
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(this);
        if (point.latitude != 0 || point.longitude != 0) {
            addresses = geocoder.getFromLocation(point.latitude ,
                    point.longitude, 1);
            String address = addresses.get(0).getAddressLine(0);
            String city = addresses.get(0).getAddressLine(1);
            String country = addresses.get(0).getAddressLine(2);
            return addresses;

        } else {
            Toast.makeText(this, "Coordinates are null",
                    Toast.LENGTH_SHORT).show();
            return null;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
于 2018-01-27T19:08:25.117 回答