Android Listview中多个点之间的距离

3

工作概述..

我正在处理一个项目,其中我们计算用户当前位置和附近用户位置之间的行驶距离。

这可能是一个重复的问题,用于单点距离计算,但我想在自定义列表视图中计算多个位置的距离。

其他附近用户位置来自Web API,具有其名称和详细信息。

为了获取两点之间的行驶距离,我使用谷歌方向API。

它会给我结果,但会拖慢应用程序,并且如果有更多数据,则会挂起设备。

要求 :

我需要在自定义列表视图中准确、流畅地找到两个位置之间的行驶距离。

我已经完成的事情 :

我找到了这个链接异步加载,但它也没有正常工作。 当滚动ListView时,应用程序会崩溃。

这里是异步加载的代码。

   @Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    viewHolder holder;

    if (convertView == null) {
        holder = new viewHolder();
    } else {
        holder = (viewHolder) convertView.getTag();
    }

    new getDistanceAsyncTask(position, holder, userLatt, userLong, sLatt1, sLong1).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);

    return convertView;
}


 private static class getDistanceAsyncTask extends AsyncTask {
    private int mPosition;
    private viewHolder mHolder;
    double Lat1, Lng1, Lat2, Lng2;

    public getDistanceAsyncTask(int position, viewHolder holder, double lat1, double lng1, double lat2, double lng2) {
        mPosition = position;
        mHolder = holder;
        this.Lat1 = lat1;
        this.Lng1 = lng1;
        this.Lat2 = lat2;
        this.Lng2 = lng2;
    }

    @Override
    protected Object doInBackground(Object... params) {
        // TODO Auto-generated method stub

        String distanceST = "";
        Double dist = 0.0;
        String uri = "http://maps.googleapis.com/maps/api/directions/json?origin=" + Lat1 + "," + Lng1 + "&destination=" + Lat2 + "," + Lng2 + "&mode=driving&sensor=false";
        String result = GET(uri);
        Log.d("result", "res=" + result);

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(result.toString());
            JSONArray array = jsonObject.getJSONArray("routes");
            JSONObject routes = array.getJSONObject(0);
            JSONArray legs = routes.getJSONArray("legs");
            JSONObject steps = legs.getJSONObject(0);
            JSONObject distance = steps.getJSONObject("distance");

            distanceST = distance.getString("text");
            dist = Double.parseDouble(distanceST.replaceAll("[^\\.0123456789]", ""));

            // Utils.showToastMsg( HomeActivity.this, "Dist is :: " + dist );
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return distanceST;
    }

    @Override
    protected void onPostExecute(Object result) {
        // TODO Auto-generated method stub
        mHolder.txtMerDistance1.setText(result.toString());

    }
}

private static String GET(String url) {
    InputStream inputStream = null;
    String result = "";
    try {
        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();
        // make GET request to the given URL
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();
        // convert inputstream to string
        if (inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "not work!";

    } catch (Exception e) {
        Log.d("InputStream", "hello" + e);
    }
    return result;
}

private static String convertInputStreamToString(InputStream inputStream) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while ((line = bufferedReader.readLine()) != null)
        result += line;
    inputStream.close();
    return result;
}

如果有最好的解决方案,请帮忙。


是什么导致了进程变慢?是调用 Google API 还是恢复用户位置? 在 getview 中使用异步任务可能是一个坏主意。这会在列表中的每次移动时调用(我想)。 - AxelH
@AxelH,是的,调用Google API会减慢应用程序的速度。我同意在getView中调用API是一个不好的想法。因此,请建议其他解决方案。 - Preet_Android
在将其传递给ListView之前应该执行此操作。当您为适配器恢复列表时,仅运行一个(或少量)异步任务而不是每个实例都运行。这需要一些时间,但是如果您设法在持有者中没有值时打印calculating,则可以使其用户友好。 - AxelH
1个回答

0

您可以使用android.location.Location类来计算距离

Location.distanceBetween()


亲爱的,它没有提供驾驶距离。它只会给出所给点之间的最短路径。 - Preet_Android
我需要在自定义列表视图中准确流畅地找出两个位置之间的距离。你询问了位置之间的距离...所以他给了你解决方案 ;) - AxelH
从适配器中加载数据并不是一个好主意。您知道应该计算距离的所有配对吗? - user2788843
是的,这显然是一个错误的概念。距离应该在发送到适配器之前计算(至少应该启动任务)。 - AxelH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接