android Listview中多个点之间的距离

时间:2016-07-01 11:58:31

标签: android performance listview async-loading

工作摘要..

我正在开展项目,我们计算用户当前位置和附近用户位置的行驶距离。

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

其他附近的用户位置来自web api及其名称和详细信息。

为了获得两点之间的行车距离我正在使用google方向api。

它会给我结果,但它会减慢应用程序的速度,如果有更多数据,那么它会挂起设备..

要求:

我需要找到自定义列表视图中两个位置之间的行驶距离准确,顺畅..

我做了什么:

我发现此链接Async Loading但它也无法正常工作。 滚动列表视图时,应用程序崩溃了。

以下是Async Loading ..的代码。

   @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;
}

如果有任何最佳解决方案,请提供帮助。

1 个答案:

答案 0 :(得分:0)

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

  

Location.distanceBetween()