【问题标题】:Google Maps, need to display country in TextView谷歌地图,需要在 TextView 中显示国家
【发布时间】:2015-10-11 18:07:04
【问题描述】:

我是谷歌地图的新手,需要在 textView 中显示用户所在的国家/地区。 目前我的应用正在显示纬度、经度和地址。现在我需要国家/地区。

我已声明如下:

private LocationManager locationManager;
private Location myLocation;

在 OnCreate 下

lblAddress = (TextView) findViewById(R.id.tvAddress);

获取我使用的地址;

private String getCompleteAddressString(double LATITUDE, double LONGITUDE, int x)
{
    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();

        }
    }
    catch (Exception e)
    {
        lblAddress.setText("Your address cannot be determined");
    }
    return strAdd;
}

现在我如何以同样的方式获得国家/地区?

【问题讨论】:

  • returnedAddress.getCountryName()
  • 您能详细说明一下吗?对不起。

标签: java android google-maps geolocation textview


【解决方案1】:

你已经有了地址,你可以在上面调用getCountryName()方法。

我要做的是创建一个 POJO 类,以便您可以从方法中返回地址和国家:

class MyLocation {
    public String address;
    public String country;
}

然后,让您的方法调用返回MyLocation 的实例,并在返回之前填充地址和国家/地区:

private MyLocation getCompleteAddressString(double LATITUDE, double LONGITUDE, int x)
{
    String strAdd = "";
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    MyLocation myLocation = new MyLocation(); //added
    try
    {
        List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
        if (addresses != null)
        {
            Address returnedAddress = addresses.get(0);
            StringBuilder strReturnedAddress = new StringBuilder("");
            myLocation.country = returnedAddress.getCountryName(); //added

            for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++)
            {
                strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
            }
            strAdd = strReturnedAddress.toString();
            myLocation.address = strAdd; //added

        }
    }
    catch (Exception e)
    {
        lblAddress.setText("Your address cannot be determined");
    }
    return myLocation; //modified
}

确保对返回值进行空检查。

    MyLocation loc = getCompleteAddressString(lat, lon, x);
    if (loc.address != null) {
        //use to populate Address TextView
    }
    if (loc.country != null) {
        //use to populate Country TextView
    }

【讨论】:

  • String strAdd = getCompleteAddressString(latitude, longitude, 1); - 当我进行建议的更改时,上面的这一部分出错了。
  • 你需要将返回值赋给一个MyLocation实例,看最后一个代码块。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-27
  • 2016-01-03
相关资源
最近更新 更多