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

时间:2014-04-15 09:42:51

标签: android gps 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();
            }

        }
    });
}

 }

6 个答案:

答案 0 :(得分:6)

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

    String country = getApplicationContext().getResources().getConfiguration().locale.getDisplayCountry();

答案 1 :(得分:6)

    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();

答案 2 :(得分: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();}

答案 3 :(得分:1)

Geocoder对象,您可以拨打getFromLocation(double, double, int) method。它将返回具有方法AddressgetLocality()个对象列表。

 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();
    }

答案 4 :(得分:1)

您可以从下面提到的json获取国家/地区名称。只需通过你的lat-long地址即可。并寻找&#34; long_name&#34;有国家,政治和#34; in&#34; types&#34;阵列。

即。 http://maps.googleapis.com/maps/api/geocode/json?address=23.022505,72.5713621&sensor=false

答案 5 :(得分: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();
}