首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么getFromLocation()的结果大小为0?如何从geopoint获得地址?- Android

为什么getFromLocation()的结果大小为0?如何从geopoint获得地址?- Android
EN

Stack Overflow用户
提问于 2015-02-28 17:56:56
回答 1查看 911关注 0票数 0

我希望将一个字符串数组从异步类返回到调用这个异步类的活动,即任务是执行反向地理编码。

因此,从我的活动中,我调用类的构造函数如下:

代码语言:javascript
运行
复制
Double[] lat_long = new Double[] { Double.parseDouble(map_lat), Double.parseDouble(map_long) };

            ReverseGeocodingTask reverseGeocoding = new ReverseGeocodingTask(getActivity().getApplicationContext());
            reverseGeocoding.execute(lat_long);

这是这个类的代码:

代码语言:javascript
运行
复制
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));
        }
    }
}

这是逻辑猫:

代码语言:javascript
运行
复制
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。

EN

回答 1

Stack Overflow用户

发布于 2015-02-28 18:03:23

这可能是GeoCoder服务的一个问题。如果您是设备的后端服务,则不存在或有其他问题,您将得到此响应。

使用isPresent检查是否存在实现。

另外,请看这里的帖子:

Geocoder.getFromLocation throws IOException on Android emulator

文档中提到您需要后端服务:

http://developer.android.com/reference/android/location/Geocoder.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28784955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档