如何从位置更改城市名称语言?
How to change city name language from location?
在我的应用程序中,我想从 Latitude 和 Longitude.
获取用户城市名称
为此,我写了下面的代码,但显示 English 语言的城市名称,例如 "Tehran"
但我想显示 Farsi 的城市名称,例如 "تهران"
如何更改语言?
我的代码:
public class MainActivity extends Activity {
private SimpleLocation mLocation;
private Geocoder geocoder;
private List<Address> addresses;
private String cityName;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// construct a new instance
mLocation = new SimpleLocation(this);
// reduce the precision to 5,000m for privacy reasons
mLocation.setBlurRadius(5000);
geocoder = new Geocoder(this, Locale.getDefault());
// if we can't access the location yet
if (!mLocation.hasLocationEnabled()) {
// ask the user to enable location access
SimpleLocation.openSettings(this);
}
final double latitude = mLocation.getLatitude();
final double longitude = mLocation.getLongitude();
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
cityName = addresses.get(0).getAddressLine(0);
} catch (IOException e) {
e.printStackTrace();
}
findViewById(R.id.textView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "" + cityName, Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onResume() {
super.onResume();
// make the device update its location
mLocation.beginUpdates();
}
@Override
protected void onPause() {
// stop location updates (saves battery)
mLocation.endUpdates();
super.onPause();
}
}
使用 string.xml 更改语言的用户本地化。请关注 https://developer.android.com/guide/topics/resources/localization.html 以了解更多关于 android 应用程序中本地化的信息。
只需在下面的构造函数中传递语言环境:
Geocoder geo = new Geocoder(MainActivity.this, new Locale("Your desired locale code"));
使用区域设置更改 TextView
Locale loc = new Locale("ar"); // change locale as per your requirement
textView.setTextLocale(loc);
textView.setText("Farsi");
不传递 Locale.getDefault()
,而是传递 new Locale("fa_IR")
。
并使用addresses.get(0).getLocality()
获取城市名称。
查看支持的语言环境列表。
在我的应用程序中,我想从 Latitude 和 Longitude.
获取用户城市名称
为此,我写了下面的代码,但显示 English 语言的城市名称,例如 "Tehran"
但我想显示 Farsi 的城市名称,例如 "تهران"
如何更改语言?
我的代码:
public class MainActivity extends Activity {
private SimpleLocation mLocation;
private Geocoder geocoder;
private List<Address> addresses;
private String cityName;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// construct a new instance
mLocation = new SimpleLocation(this);
// reduce the precision to 5,000m for privacy reasons
mLocation.setBlurRadius(5000);
geocoder = new Geocoder(this, Locale.getDefault());
// if we can't access the location yet
if (!mLocation.hasLocationEnabled()) {
// ask the user to enable location access
SimpleLocation.openSettings(this);
}
final double latitude = mLocation.getLatitude();
final double longitude = mLocation.getLongitude();
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
cityName = addresses.get(0).getAddressLine(0);
} catch (IOException e) {
e.printStackTrace();
}
findViewById(R.id.textView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "" + cityName, Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onResume() {
super.onResume();
// make the device update its location
mLocation.beginUpdates();
}
@Override
protected void onPause() {
// stop location updates (saves battery)
mLocation.endUpdates();
super.onPause();
}
}
使用 string.xml 更改语言的用户本地化。请关注 https://developer.android.com/guide/topics/resources/localization.html 以了解更多关于 android 应用程序中本地化的信息。
只需在下面的构造函数中传递语言环境:
Geocoder geo = new Geocoder(MainActivity.this, new Locale("Your desired locale code"));
使用区域设置更改 TextView
Locale loc = new Locale("ar"); // change locale as per your requirement
textView.setTextLocale(loc);
textView.setText("Farsi");
不传递 Locale.getDefault()
,而是传递 new Locale("fa_IR")
。
并使用addresses.get(0).getLocality()
获取城市名称。
查看支持的语言环境列表。