我想计算两个位置(即用户和目的地位置的当前位置)之间的距离。我有我的数据库和当前位置的目标位置的经度和纬度(我确实找到位置管理器类)。 我需要在列表视图中显示计算出的距离。 如果我计算它的任何数学公式或DistanceTo()和DistanceBetween()方法的位置类,然后我没有得到准确的距离作为超过地图。 如果我通过Google的距离矩阵API计算距离,那么它计算距离和加载列表需要花费太多时间(因为有很多目标位置和用户的当前位置) 请有人向我推荐最好的方式来计算它.. 我在这个问题从最近几天卡住..帮我 在此先感谢...我们怎样才能计算出当前位置与android中的许多目标位置之间的距离?
回答
很多谷歌上搜索我已经做了我task.I尝试过通过异步任务.. 这里是我的代码后,最后..
public View getView(int position, View convertView, ViewGroup Parent)
{
final ViewHolder holder=new ViewHolder();
View view=convertView;
if (view==null) {
convertView= inflater.inflate(R.layout.layout_row, null);
}
holder.textdistance=(TextView) convertView.findViewById(R.id.textView_Distance);
holder.position = position;
if(data.size()<=0)
{
Toast.makeText(activity, "No data", Toast.LENGTH_LONG).show();
}
else
{
**new ThumbnailTask(position, holder)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);**
}
return convertView;
}
//Thumbnail Class
private static class ThumbnailTask extends AsyncTask {
private int mPosition;
private ViewHolder mHolder;
public ThumbnailTask(int position, ViewHolder holder) {
mPosition = position;
mHolder = holder;
}
/* (non-Javadoc)
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
String Distance="3";
String uri="https://maps.googleapis.com/maps/api/distancematrix/json? origins=(Your source Addres like XYZ,South Delhi-110064),&mode=deriving&sensor=false&key=(Your API Key)";
String result= GET(uri);
Log.d("result","res="+result);
try {
//jArray = new JSONObject(result);
JSONObject object = (JSONObject) new JSONTokener(result).nextValue();
JSONArray array = object.getJSONArray("rows");
// Log.d("JSON","array: "+array.toString());
//Routes is a combination of objects and arrays
JSONObject rows = array.getJSONObject(0);
//Log.d("JSON","routes: "+routes.toString());
JSONArray elements = rows.getJSONArray("elements");
// Log.d("JSON","legs: "+legs.toString());
JSONObject steps = elements.getJSONObject(0);
//Log.d("JSON","steps: "+steps.toString());
JSONObject distance = steps.getJSONObject("distance");
Log.d("JSON","distance: "+distance.toString());
Distance = distance.getString("text");
Log.d("final value","tot dis="+Distance);
JSONObject duration=steps.getJSONObject("duration");
// MyDuration=duration.getString("text");
}
catch(JSONException e)
{
Log.d("JSONEXCeption","my exce"+e.getMessage());
}
return Distance;
}
@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
mHolder.textdistance.setText(result.toString());
}
}
//method to execute Url
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);
// Log.i("Result",result);
else
result = "Did 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;
}
}
参考链接为─
http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/ https://developers.google.com/maps/documentation/distancematrix/
我不擅长爆炸但是我正在尽我所能。希望这会帮助像我这样的其他人,因为我陷入了这个任务。
试试这个。我加我的旧应用程序中。它适用于我。通过初始化R=Radius
。阅读this文章仔细
public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) {
double lat1 = StartP.getLatitudeE6()/1E6;
double lat2 = EndP.getLatitudeE6()/1E6;
double lon1 = StartP.getLongitudeE6()/1E6;
double lon2 = EndP.getLongitudeE6()/1E6;
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(lon2-lon1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.asin(Math.sqrt(a));
return Radius * c;
}
嗯谢谢,我已经尝试过它已经如我上面提到的数学公式(在这我试了相同的)。它给出不同的输出与地图上的输出。 –
@kirtiHudda请点击此链接http://stackoverflow.com/questions/22351984/how-to-find-the-distance-between-two-points-along-a- specific-route?rq = 1 –
如果有很多地方使用循环。只有几行代码来完成这件事:
Location l1=new Location("One");
l1.setLatitude(location.getLatitude());
l1.setLongitude(location.getLongitude());
Location l2=new Location("Two");
l2.setLatitude(Double.parseDouble(frnd_lat));
l2.setLongitude(Double.parseDouble(frnd_longi));
float distance_bw_one_and_two=l1.distanceTo(l2);
完美答案:) – Pulkit
- 1. 从我的位置到目标位置的距离计算android
- 2. 用户的位置距离我目前的位置的距离
- 3. 计算用户位置和固定位置之间的距离
- 4. 我怎样才能在Android当前的位置?
- 5. 获取当前位置和计算出的路线之间的距离
- 6. 获取当前位置和android中的所选位置之间的距离
- 7. 计算我的位置到多个位置的距离
- 8. 使用lattitude和当前位置和目标位置的经度,我们怎样才能在iphone
- 9. 计算Java中多个地理位置之间的距离
- 10. 计算距离位置的距离 - iPhone
- 11. 如何计算我的当前位置和填充在JSON文件中的位置之间的距离
- 12. 我们怎样才能减少两条边之间的距离?
- 13. 如何获得我的当前位置和新位置之间的距离?
- 14. 计算2个位置之间的距离并计算价格
- 15. 计算两个位置之间的距离Android Studio
- 16. Android:计算两个位置之间距离的最佳方法
- 17. Java:计算大量位置和性能之间的距离
- 18. 我该如何计算两个位置之间的距离
- 19. 谷歌地图在距离计算器中的当前位置
- 20. 从我的当前位置获取距离最近的位置
- 21. 我怎样才能得到textarea中的行号的当前光标位置?
- 22. 从我当前位置到另一个位置的距离
- 23. 计算基于道路的两个位置之间的距离
- 24. 通过json计算用户位置和餐厅位置之间的距离(url)
- 25. 计算从底部到当前位置的距离
- 26. 我怎样才能找到一个位置附近的位置?
- 27. 使用html5测量特定位置和当前位置之间的距离
- 28. 找到当前位置和指定位置之间的道路距离
- 29. 计算用户位置与其他位置之间的距离和时间点击Google地图Android API V2
- 30. 计算Android地图中两个位置之间的距离和时间V2
检查此链接http://stackoverflow.com/questions/10683554/distance-calculation-from-my-location-to-destination-location-in- Android –
Hello @ Amiya ..首先感谢您的支持。我已经通过五种方式尝试了它。1.DistanceTo()2.Distance between()3.Mathematical Formulas 4.Google Map API V3 5.Google Map API V2.现在1到4的问题没有得到正确的输出,并且使用Google API V2会花费很多时间,因为我计算的是距离用户当前位置近100个位置的距离,然后将该距离加载到列表中-视图。由于处理时间过长,我的列表视图不能正确滚动 –
DistanceTo()和DistanceBetween()总是返回两个坐标之间的位移,这就是为什么你没有得到准确的结果。所以你可以做的是将其他数学公式在AsyncTask或使用线程来计算距离,只要确保你没有阻止主线程。祝你好运! – Pankaj