如何在Android中获取国家名称?

4

你好,我正在从事Android开发。我已经创建了一个GPS应用程序来获取当前位置。现在,如何从纬度和经度值中获取国家名称?这可行吗?请帮助我,谢谢:)

以下是我使用的代码:

 public class AndroidGPSTrackingActivity extends Activity {

Button btnShowLocation;

// GPSTracker class
GPSTracker gps;
String countryCode,countryName;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnShowLocation = (Button) findViewById(R.id.btnShowLocation);

    // show location button click event
    btnShowLocation.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {        
            // create class object
            gps = new GPSTracker(AndroidGPSTrackingActivity.this);

            // check if GPS enabled     
            if(gps.canGetLocation()){

                double latitude = gps.getLatitude();
                double longitude = gps.getLongitude();
                Log.i("location", " "+latitude+" "+longitude);
                // \n is for new line
                try
                {Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault());
                List<Address> addresses = gcd.getFromLocation(latitude,longitude, 1);
                if (addresses.size() > 0) 
                    Toast.makeText(getApplicationContext(), "name "+addresses.get(0).getLocality(),Toast.LENGTH_SHORT).show();

                    //System.out.println(addresses.get(0).getLocality());
                }

                     catch (IOException e) {

                            e.printStackTrace();
                }
                //Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude+countryName +"  "+countryCode, Toast.LENGTH_LONG).show();    
            }else{
                // can't get location
                // GPS or Network is not enabled
                // Ask user to enable GPS/network in settings
                gps.showSettingsAlert();
            }

        }
    });
}

 }

可能是重复的问题:GPS坐标对应的国家名称 - M D
也许这可以帮助你:从坐标获取国家? - Alex DG
我使用了上述链接,结果显示为null。 - Maxwell
6个回答

10
    String country_name = null;
    LocationManager lm = (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    Geocoder geocoder = new Geocoder(getApplicationContext());
    for(String provider: lm.getAllProviders()) {
        @SuppressWarnings("ResourceType") Location location = lm.getLastKnownLocation(provider);
        if(location!=null) {
            try {
                List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                if(addresses != null && addresses.size() > 0) {
                    country_name = addresses.get(0).getCountryName();
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    Toast.makeText(getApplicationContext(), country_name, Toast.LENGTH_LONG).show();

8
最终我找到了解决方案,没有经纬度值,我得到了国家名称。
    String country = getApplicationContext().getResources().getConfiguration().locale.getDisplayCountry();

.


6
但这会将“美国”作为所有人的国家,这不会提供用户所在位置。 - silverFoxA
这不是基于当前位置,而是基于当前语言环境。我可能在中国使用en_GB语言环境。这会返回GB。 - Snebhu

2
使用这个:
    Geocoder myLocation = new Geocoder(AppContext);
            try
            {
                myList = myLocation.getFromLocation(latitude, longitude, 1);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            if(myList != null)
            {
                try
                {


                    String country = myList.get(0).getCountryName();}

1
Geocoder对象中,您可以调用getFromLocation(double, double, int)方法。它将返回一个Address对象列表,其中包含一个getLocality()方法
 import android.location.Address;
import android.location.Geocoder;
Geocoder gcd = new Geocoder(this, Locale.getDefault());
    List<Address> addresses;
    try {
        addresses = gcd.getFromLocation(1.2, 2.2256, 1);
         if (addresses.size() > 0) 
                System.out.println(addresses.get(0).getLocality());
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

尝试一下我的新答案。 - Bhanu Sharma
是的,我已经创建并尝试获取地址,但它显示为空。 - Maxwell
兄弟,它在我的电脑上运行得非常好,你能实现相同的效果吗? - Bhanu Sharma
尝试将此相同的代码添加到onCreate()中,而不是在按钮单击中,并粘贴我给你的相同代码,只更改原始或真实的纬度和经度,并且不要使用toast打印,只需尝试一次。 - Bhanu Sharma
我只想知道按钮点击是否有任何错误,或者只是为了确认。 - Bhanu Sharma
显示剩余4条评论

1

0
List<Address> addresses=null;
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
    addresses = geocoder.getFromLocation(loc.latitude, loc.longitude, 1);
    System.out.println("add in string "+addresses.toArray().toString());
    String countryName = addresses.get(0).getCountryName();
    String countryCode = addresses.get(0).getCountryCode();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接