在android上使用经度和纬度指定国家名称

在android上使用经度和纬度指定国家名称,android,google-maps,google-maps-api-3,Android,Google Maps,Google Maps Api 3,我在android模拟器上使用google api 15 我想通过触摸屏幕从地图上指定一个国家, 所以如果我触摸屏幕上的经度和纬度,我就能知道它们属于哪个国家或城市 。。非常感谢。您可以尝试以下内容: public static String getCountryName(Context context, double latitude, double longitude) { Geocoder geocoder = new Geocoder(context, Locale.getDef

我在android模拟器上使用google api 15

我想通过触摸屏幕从地图上指定一个国家, 所以如果我触摸屏幕上的经度和纬度,我就能知道它们属于哪个国家或城市


。。非常感谢。

您可以尝试以下内容:

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
    }

}
公共静态字符串getCountryName(上下文、双纬度、双经度){
Geocoder Geocoder=新的地理编码器(上下文,Locale.getDefault());
列表地址=空;
试一试{
地址=地理编码器.getFromLocation(纬度,经度,1);
地址结果;
if(addresses!=null&&!addresses.isEmpty()){
返回地址。get(0.getCountryName();
}
返回null;
}捕获(忽略IOException){
//做点什么
}
}

您尝试过什么吗?如果是,请分享您尝试过的代码或想法。如果你想依赖谷歌api,你需要反向地理编码并从结果中提取国家部分。好吧,我忘了包括我的尝试,谢谢你的提示,是的,我终于找到了一个解决方案,就像你所说的反向地理编码:):。@Cristian我想知道你能否帮我解决这个问题。我保证,这不是那种“为我解决这个问题”的问题。这不是一直有效的。它在一些安卓设备上不起作用。不确定getFromLocation为什么不一直检索地址。