gpt4 book ai didi

java - 如何从位置更改城市名称语言?

转载 作者:行者123 更新时间:2023-12-01 19:57:10 24 4
gpt4 key购买 nike

在我的应用程序中,我想从纬度经度获取用户城市名称。
为此,我编写了下面的代码,但显示了英语语言的城市名称,例如“德黑兰”

但我想显示这个波斯语的城市名称,例如“Tuهrān”

如何更改语言?

我的代码:

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

最佳答案

只需在下面的构造函数中传递区域设置:

Geocoder geo = new Geocoder(MainActivity.this, new Locale("Your desired locale code"));

关于java - 如何从位置更改城市名称语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49251018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com