我希望将一个字符串数组从异步类返回到调用这个异步类的活动,即任务是执行反向地理编码。
因此,从我的活动中,我调用类的构造函数如下:
Double[] lat_long = new Double[] { Double.parseDouble(map_lat), Double.parseDouble(map_long) };
ReverseGeocodingTask reverseGeocoding = new ReverseGeocodingTask(getActivity().getApplicationContext());
reverseGeocoding.execute(lat_long);
这是这个类的代码:
class ReverseGeocodingTask extends AsyncTask<Double, Void, List<String>> {
public static List<String> LIST = new ArrayList<String>();
Context mContext;
public ReverseGeocodingTask(Context context) {
super();
mContext = context;
}
@Override
protected List<String> doInBackground(Double... params) {
Geocoder gc= new Geocoder(mContext, Locale.getDefault());
List<Address> addrList = null;
double latitude = params[0].doubleValue();
double longitude = params[1].doubleValue();
Log.d("LATLONG", latitude + ":" + longitude);
try {
addrList = gc.getFromLocation(latitude, longitude, 1);
if (addrList.size() > 0) {
//format location info
Address address = addrList.get(0);
LIST.add(address.getLocality());
LIST.add(address.getSubAdminArea());
LIST.add(address.getCountryName());
Log.d("LIST", LIST.get(0));
}
else{
Log.d("addrList SIZE", "=0");
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
return LIST;
}
@Override
protected void onPostExecute(List<String> result) {
if (result != null) {
Log.d("ON POST", result.get(0));
}
}
}
这是逻辑猫:
02-28 19:20:04.323 12275-14109/guide_me_for_all.guide_me_for_all D/LATLONG﹕ 34.681377999999995:33.039339
02-28 19:20:05.434 12275-14109/guide_me_for_all.guide_me_for_all D/addrList SIZE﹕ =0
我正确地得到了从Log.d()
中可以看到的纬度和经度点,但是getFromLocation.size()
总是0。
发布于 2015-02-28 18:03:23
这可能是GeoCoder
服务的一个问题。如果您是设备的后端服务,则不存在或有其他问题,您将得到此响应。
使用isPresent
检查是否存在实现。
另外,请看这里的帖子:
Geocoder.getFromLocation throws IOException on Android emulator
文档中提到您需要后端服务:
http://developer.android.com/reference/android/location/Geocoder.html
https://stackoverflow.com/questions/28784955
复制相似问题