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

Max*_*ell 4 gps android location

嗨我正在使用android.I创建了一个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();
            }

        }
    });
}

 }
Run Code Online (Sandbox Code Playgroud)

小智 9

    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();
Run Code Online (Sandbox Code Playgroud)


Max*_*ell 8

最后我得到了解决方案,没有lat和long值,我得到了国名.

    String country = getApplicationContext().getResources().getConfiguration().locale.getDisplayCountry();
Run Code Online (Sandbox Code Playgroud)

.

  • 但这给了"美国"作为所有人的国家,这不会给用户所在的位置 (5认同)