如何使用 Geocoder 提取位置属性?
How to extract location attributes using Geocoder?
当我尝试获取默认语言环境时 returns "zz_ZZ":
国家代码:"ZZ"
语言代码:"zz"
代码是:
private String getAddressFromLocation(Location location) {
Geocoder geoCoder = new Geocoder(this.getApplicationContext(), Locale.getDefault());
List<Address> matches = null;
try {
matches = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
Address bestMatch = (matches.isEmpty() ? null : matches.get(0) );
if (bestMatch != null){
String fullAddress = bestMatch.getAddressLine(0);
for (int i = 1; i <= bestMatch.getMaxAddressLineIndex(); i++){
fullAddress += ", " + bestMatch.getAddressLine(i);
}
return fullAddress;
}else{
return Constants.EMPTY_STRING;
}
} catch (IOException e) {
ACRA.getErrorReporter().handleSilentException(new MyGeoLocationAddressException(TAG + " ERROR CREATING ADDRESS - " + e.getMessage()));
return Constants.EMPTY_STRING;
}
}
我需要设置DefaultLocale吗? ZZ_zz 是什么意思?
第二个问题是:
如果我使用 "ZZ_zz" 默认语言环境,我不会得到任何地址,但如果我使用:
Locale.setDefault(Locale.ENGLISH);
我得到了确切的地址。
ZZ_zz
是 带有口音的英语 语言环境的标识符,主要用于查找应用中缺失的翻译。
我认为没有关于如何处理带有口音的英语的最佳实践,但将其视为英语可能是最佳选择。
必须这样处理location,查看无返回地址时的场景
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(
location.getLatitude(),
location.getLongitude(),
// In this sample, get just a single address.
1);
} catch (IOException ioException) {
// Catch network or other I/O problems.
errorMessage = getString(R.string.service_not_available);
Log.e(TAG, errorMessage, ioException);
} catch (IllegalArgumentException illegalArgumentException) {
// Catch invalid latitude or longitude values.
errorMessage = getString(R.string.invalid_lat_long_used);
Log.e(TAG, errorMessage + ". " +
"Latitude = " + location.getLatitude() +
", Longitude = " +
location.getLongitude(), illegalArgumentException);
}
// Handle case where no address was found.
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<String>();
// Fetch the address lines using getAddressLine,
// join them, and send them to the thread.
for(int i = 0; i < address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
Log.i(TAG, getString(R.string.address_found));
deliverResultToReceiver(Constants.SUCCESS_RESULT,
TextUtils.join(System.getProperty("line.separator"),
addressFragments));
}
}
当我尝试获取默认语言环境时 returns "zz_ZZ":
国家代码:"ZZ" 语言代码:"zz"
代码是:
private String getAddressFromLocation(Location location) {
Geocoder geoCoder = new Geocoder(this.getApplicationContext(), Locale.getDefault());
List<Address> matches = null;
try {
matches = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
Address bestMatch = (matches.isEmpty() ? null : matches.get(0) );
if (bestMatch != null){
String fullAddress = bestMatch.getAddressLine(0);
for (int i = 1; i <= bestMatch.getMaxAddressLineIndex(); i++){
fullAddress += ", " + bestMatch.getAddressLine(i);
}
return fullAddress;
}else{
return Constants.EMPTY_STRING;
}
} catch (IOException e) {
ACRA.getErrorReporter().handleSilentException(new MyGeoLocationAddressException(TAG + " ERROR CREATING ADDRESS - " + e.getMessage()));
return Constants.EMPTY_STRING;
}
}
我需要设置DefaultLocale吗? ZZ_zz 是什么意思?
第二个问题是:
如果我使用 "ZZ_zz" 默认语言环境,我不会得到任何地址,但如果我使用:
Locale.setDefault(Locale.ENGLISH);
我得到了确切的地址。
ZZ_zz
是 带有口音的英语 语言环境的标识符,主要用于查找应用中缺失的翻译。
我认为没有关于如何处理带有口音的英语的最佳实践,但将其视为英语可能是最佳选择。
必须这样处理location,查看无返回地址时的场景
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(
location.getLatitude(),
location.getLongitude(),
// In this sample, get just a single address.
1);
} catch (IOException ioException) {
// Catch network or other I/O problems.
errorMessage = getString(R.string.service_not_available);
Log.e(TAG, errorMessage, ioException);
} catch (IllegalArgumentException illegalArgumentException) {
// Catch invalid latitude or longitude values.
errorMessage = getString(R.string.invalid_lat_long_used);
Log.e(TAG, errorMessage + ". " +
"Latitude = " + location.getLatitude() +
", Longitude = " +
location.getLongitude(), illegalArgumentException);
}
// Handle case where no address was found.
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<String>();
// Fetch the address lines using getAddressLine,
// join them, and send them to the thread.
for(int i = 0; i < address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
Log.i(TAG, getString(R.string.address_found));
deliverResultToReceiver(Constants.SUCCESS_RESULT,
TextUtils.join(System.getProperty("line.separator"),
addressFragments));
}
}