有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java地理记录器:无法在getFromLocation()中传递LatLng

现在,我试图获取一个位置的国家/地区名称,但无法在getFromLocation()中传递LatLng。 我怎样才能解决这个问题

public void checkCountry(LatLng location) {
     Geocoder gcd = new Geocoder(this, Locale.getDefaut());
     List<Address> addresses = gcd.getFromLocation(location.latitude, location.logtitude, 1); //error here
     String country = addresses.get(0).getCountryName();

错误是

Unhandled Exception: java.IO.Exception

getFromLocation() cannot be applied to: latitude double location.latitude longtitude double location.longtitude

我怎么了


共 (1) 个答案

  1. # 1 楼答案

    只需处理异常。要么扔,要么抓

    public void checkCountry(LatLng location) {
        Geocoder gcd = new Geocoder(this, Locale.getDefaut());
        List<Address> addresses=new ArrayList<>();
        try{
            addresses=  gcd.getFromLocation(location.latitude, location.longitude, 1); //error here
        }catch (Exception e){
            e.printStackTrace();
        }
        String country;
        if(addresses!=null)if(addresses.size()!=0) country= addresses.get(0).getCountryName();
    }