如何在android中从经纬度仅获取城市,州,国家 [英] How to get only City, State, Country from lat, long in android

查看:135
本文介绍了如何在android中从经纬度仅获取城市,州,国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码将经纬度(lat)转换为人类可读的地址.现在,iam可获得包括街道名称在内的完整详细信息.我怎样才能只得到城市,州,国家?我不想要任何更多细节.请帮我.

I have the below code to convert lat, long to human readable address. Now iam getting full details including street name. How can i get only city, state, country? I don't want anything more details. Please help me.

Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try {
   List<Address> addresses = geoCoder.getFromLocation(latitude, longitude, 1);

   String add = "";
   if (addresses.size() > 0) 
   {
      for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
     add += addresses.get(0).getAddressLine(i) + "\n";
   }

   showToastMessage(add);
}
catch (IOException e1) {                
   e1.printStackTrace();
}   

推荐答案

反向地理编码的位置描述中的详细信息量可能会有所不同,例如一个可能包含最近建筑物的完整街道地址,而另一个可能仅包含一个建筑物的完整街道地址.城市名称和邮政编码.这将返回城市名称和国家名称

The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code. This will return the city name and country name

    if (addresses.size() > 0) {
        System.out.println(addresses.get(0).getLocality());
        System.out.println(addresses.get(0).getCountryName());
    }

有关详细信息,您可以查看地址对象

For details you can look into the Address object

这篇关于如何在android中从经纬度仅获取城市,州,国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆