在android上使用经度和纬度指定国家/地区的名称

时间:2012-03-30 18:59:07

标签: android google-maps google-maps-api-3

我在Android模拟器上使用谷歌api 15。

我想通过触摸屏幕来指定地图中的国家/地区, 因此,如果我在某个经度和纬度上触摸屏幕,我可以知道属于哪个国家或城市?

..非常感谢。

1 个答案:

答案 0 :(得分:14)

您可以尝试这样的事情:

public static String getCountryName(Context context, double latitude, double longitude) {
    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
    List<Address> addresses = null;
    try {
        addresses = geocoder.getFromLocation(latitude, longitude, 1);
        Address result;

        if (addresses != null && !addresses.isEmpty()) {
            return addresses.get(0).getCountryName();
        }
        return null;
    } catch (IOException ignored) {
       //do something
    }

}