android 获取位置名称,关于android:以所在国家/地区的语言获取位置名称

我正在开发一个显示世界各地公园的android应用,但我遇到了下一个问题:

在我的应用程序中,我希望用户在地图上选择他正在创建的新公园在此时的位置,并且我希望其他人无论身在何处都能看到他的公园。因此,为了做到这一点,我需要确保公园的位置名称使用公园所在国家/地区的语言。例如,如果公园位于希腊,则其位置名称将为希腊语。如果公园位于美国,则其名称将为英文。

因此,为了使其成为可能,我需要能够获取公园地址的语言环境。

为了获得公园的名称,我使用以下代码:

public String getAddressName(Latlng latLng){

Geocoder geocoder;

List

addresses;

geocoder = new Geocoder(this, Locale.getDefault());

try{

addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);

geocoder = new Geocoder(this, addresses.get(0).getLocale());

addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);

if (addresses.size() > 0)

return addresses.get(0).getAddressLine(0);

}

return null;

}

问题在于此代码以电话默认语言返回位置。

为了修复它,我需要替换当前latLng的语言环境中的行Locale.getDefault()。

我试图更改代码并执行

geocoder = new Geocoder(this, Locale.getDefault());

addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);

geocoder = new Geocoder(this, addresses.get(0).getLocale());

addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);

但这是行不通的,因为addresses.get(0).getLocale()返回电话语言环境,而不是地址实际语言环境。

如果你们中的任何一个可以给我关于如何解决我的问题的任何建议,我将很乐意听到。谢谢!

您需要选择重点语言吗?

是。 如果我使用的是语言,则可以选择位置名称的语言

最终我找到了解决方案。

我已经使用了类似问题的答案。

该解决方案与MeosCoder对这个问题的回答非常相似。

现在,我的代码如下所示:

Geocoder geocoder;

List

addresses;

geocoder = new Geocoder(this, Locale.getDefault());

String markerAddress ="";

try {

addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);

String langCode = null;

String countryCode = null;

if (addresses.size() > 0) {

countryCode = addresses.get(0).getCountryCode();

addresses = null;

Locale[] locales = Locale.getAvailableLocales();

for (Locale localeIn : locales) {

if (countryCode.equalsIgnoreCase(localeIn.getCountry())) {

langCode = localeIn.getLanguage();

break;

}

}

if (langCode != null) {

Locale locale = new Locale(langCode, countryCode);

geocoder = new Geocoder(this, locale);

try {

addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);

} catch (IOException | IndexOutOfBoundsException | NullPointerException ex) {

addresses = null;

}

}

}

尝试这个。 您可以从latlon位置获取语言代码或语言名称。

public String getLanguageCode(double lat, double lon){

Geocoder geocoder;

List

addresses;

geocoder = new Geocoder(this, Locale.getDefault());

String langCode = null;

try{

addresses = geocoder.getFromLocation(lat, lon, 1);

Address address = addresses.get(0);

String countryCode = address.getCountryCode();

Locale[] locales = Locale.getAvailableLocales();

for (Locale localeIn : locales) {

if (countryCode.equalsIgnoreCase(localeIn.getCountry())) {

//langCode = localeIn.getLanguage(); //This is language code, ex : en

langCode = localeIn.getDisplayLanguage();// This is language name, ex : English

break;

}

}

} catch (Exception ex) {

}

return langCode;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值