gpt4 book ai didi

android - android Listview中多个点之间的距离

转载 作者:太空狗 更新时间:2023-10-29 14:47:39 25 4
gpt4 key购买 nike

工作总结..

我正在做一个项目,我们计算从用户当前位置到附近用户位置的行驶距离。

单点距离计算可能是一个重复的问题,但我想在自定义 ListView 中计算多个位置的距离。

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

为了获得两点之间的行驶距离,我使用的是 google direction api。

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

要求:

我需要在自定义 ListView 中准确、顺利地找出两个位置之间的行驶距离..

我做了什么:

我找到了这个链接 Async Loading,但它也无法正常工作。滚动 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;
}

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

最佳答案

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

Location.distanceBetween()

关于android - android Listview中多个点之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38144863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com