Geocoder返回长度为零的地址 [英] Geocoder returns an address with zero length
问题描述
Geocoder直到今天都工作良好.它开始返回长度为0的String. 注意:这不是Geocoder线程的重复,我在启动线程之前使用了Intent服务和AsyncTask来获取它(并且AsyncTask方法在8个月内工作正常),选中了代码(这是官方代码),它还返回零长度的字符串.
Geocoder was working fine until today. It started to return String with 0 length. Note: This is not a duplicate of Geocoder threads, i used intent service and AsyncTask to get it before starting the thread(and AsyncTask approach was working fine over 8 months), checked this code and new code from Google with FusedLocationProviderClient(this is offical code) it also returns string with zero length.This link from official Android page shows how to get it with intent service.
我在Android 5.1上获得完整的地址,但在Android 7.1上,它返回的地址长度为零.
I get full address on Android 5.1 but on Android 7.1 it returns an address with length zero.
我以前使用过的代码,直到今天都可以正常工作.
Code i used to use and worked fine until today.
private void getAddressFromCoordinates() {
new AsyncTask<Void, String, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
addresses.clear();
addresses.addAll(geocoder.getFromLocation(mCurrentLocation.getLatitude(),
mCurrentLocation.getLongitude(), 1));
} catch (IOException e) {
e.printStackTrace();
showToastAsync(getString(R.string.activity_loc_no_location_info));
} catch (IllegalArgumentException illegalArgumentException) {
// Catch invalid latitude or longitude values.
showToastAsync("Invalid latitude or longitude values");
}
// Handle case where no address was found.
if (addresses == null || addresses.size() == 0) {
showToastAsync(getString(R.string.activity_loc_no_address_is_found));
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<String>();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
addressInfo = TextUtils.join(System.getProperty("line.separator"), addressFragments);
addressSingleLine = LocationActivity.addressInfo.replaceAll("[\r\n]+", " ");
}
return null;
}
}.execute();
}
答案是将=
添加到for循环中,以便在Android 7上不检索长度为零的地址.您不需要在某些版本上执行此操作.在Google引用的Github页面上的代码也缺少 = .这就是为什么我找不到原因的原因.
Answer is to add =
to for loop to not retreive an address with zero length on Android 7. You don't need to do this on some versions. Also code on Google referenced Github pages is missing =. That's why i was not able to find the reason why.
推荐答案
问题是此行:
for(int i = 0; i< address.getMaxAddressLineIndex(); i ++)
必须为: for(int i = 0; i< = address.getMaxAddressLineIndex(); i ++) 因为getMaxAddressLineIndex返回最大的索引.
It must be : for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) Because getMaxAddressLineIndex returns the largest index.
这篇关于Geocoder返回长度为零的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!