如何在android中获取国家/地区名称? [英] How to get country name in android?
本文介绍了如何在android中获取国家/地区名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我正在使用android.I创建了一个GPS应用程序以获取当前位置。现在如何从经度和纬度值获取国家/地区名称?可能吗 ?请帮助我,并感谢:)
这里是我使用的代码
public class AndroidGPSTrackingActivity extends Activity {
Button btnShowLocation;
// GPSTracker类
GPSTracker gps;
String countryCode,countryName;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowLocation =(Button)findViewById(R.id.btnShowLocation);
//显示位置按钮点击事件
btnShowLocation.setOnClickListener(new View.OnClickListener(){
$ b $ @Override
public void onClick(View arg0 ){
//创建类对象
gps = new GPSTracker(AndroidGPSTrackingActivity.this);
//检查GPS是否启用
if(gps.canGetLocation() ){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Log.i(location,+ latitude + + longitude);
// \\\
用于换行
try
{Geocoder gcd = new Geocoder(getApplicationContext(),Locale.getDefault());
List< Address> addresses = gcd.getFromLocation(latitude,longitude,1);
if(addresses.size()> 0)
Toast.makeText(getApplicationContext(),name+ add resses.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 - \\\
Lat:+ latitude +\\\
Long:+ longitude + countryName ++ countryCode,Toast。 LENGTH_LONG).show();
} else {
//无法获取位置
// GPS或网络未启用
//请求用户在设置中启用GPS /网络
gps .showSettingsAlert();
}
}
});
}
}
解决方案
String country = getApplicationContext() 。.getResources()getConfiguration()locale.getDisplayCountry();
。
Hi I am working with android.I had created a GPS app for getting the current location.Now How can I get the country name from the latitude and longitude value ? is it possible ? please help me and thanks :)
here is my code I used
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();
}
}
});
}
}
解决方案
Finally I got the solution, without lat and long value, i got country name.
String country = getApplicationContext().getResources().getConfiguration().locale.getDisplayCountry();
.
这篇关于如何在android中获取国家/地区名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文