我目前正在开发一个专注于健身的应用程序。我的基本想法是允许人们跟踪他们的速度、距离和时间。到目前为止,我已经通过使用位置管理器getSpeed()设法获得了速度。我想知道如何获得旅行的距离?我寻找了一些示例,但对我来说有点困惑,因为我刚刚开始使用android。我将感谢任何帮助或建议,谢谢
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_track);
start = (Button) findViewById(R.id.btnStart);
speed = (TextView)findViewById(R.id.txtSpeed);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//initialize location listener
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
getSpeed(location);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
//get the speed from the given location updates
public void getSpeed(Location location) {
currentSpeed = (location.getSpeed()*3600/1000);
String convertedSpeed = String.format("%.2f",currentSpeed);
speed.setText(convertedSpeed + "Km/h");
}
public void getDistance(Location location){
}
};
//*****this is for log in and gcm reg id passsing in back end
private class communicate_server_for_get_2_position_duration extends AsyncTask<String, Void, String> {
static final String TAG = "rvms";
Context getcontext;
String lat1 = null, lng1 = null, lat2 = null, lng2 = null;
String getcontent_for_validate, content;
public communicate_server_for_get_2_position_duration(Context context, double lat, double lng, double lat2, double lng2) {
getcontext = context;
lat1 = String.valueOf(lat);
lng1 = String.valueOf(lng);
this.lat2 = String.valueOf(lat2);
this.lng2 = String.valueOf(lng2);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
try {
} catch (Exception e) {
finish();
}
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
BufferedReader reader;
String url_data = null;
try {
url_data = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + lat1 + "," + lng1 + "&destinations=" + lat2 + "," + lng2 + "&mode=driving&language=en-EN";
url_data = url_data.replace("\n", "%20");
url_data = url_data.replace(" ", "%20");
url_data = url_data.replace("'", "%20");
} catch (Exception e) {
}
Log.i("pavan", "url" + url_data);
try {
URL url = new URL(url_data);
URLConnection conection = url.openConnection();
conection.setConnectTimeout(5000);
conection.setDoOutput(true);
reader = new BufferedReader(new InputStreamReader(conection.getInputStream()));
StringBuilder result = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
result.append(line + "");
}
content = result.toString();
System.out.print(result);
System.out.println(content);
getcontent_for_validate = content;
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
@Override
protected void onPostExecute(String result) {
JSONObject jsonobj;
// TODO Auto-generated method stub
super.onPostExecute(result);
//Toast.makeText(context,"response "+result,Toast.LENGTH_LONG).show();
String distance = null, duration = null;
if (result != null) {
if (content.equals("failure")) {
} else {//ths is getting data for vehicl_list_unread_count code, client id,restapi_key
JSONObject location1, location2;
String location_string1 = null;
try {
JSONObject ret = new JSONObject(getcontent_for_validate);
// Toast.makeText(getApplicationContext(),"this is get content"+jsonobj.toString(),Toast.LENGTH_SHORT).show();
// System.out.println("this is get content"+jsonobj.toString());
try {
location1 = ret.getJSONArray("rows").getJSONObject(0);
location2 = location1.getJSONArray("elements").getJSONObject(0);
JSONObject get_distance = location2.getJSONObject("distance");
distance = get_distance.getString("text");
JSONObject get_duration = location2.getJSONObject("duration");
duration = get_duration.getString("text");
Log.d("test", "formattted address:" + location_string1);
} catch (JSONException e1) {
e1.printStackTrace();
}
} catch (JSONException e1) {
e1.printStackTrace();
}
try {
if (key_for_intent_data_from.equals("from current location")) {
//FabBtn.show (); //String formattedDate=new SimpleDateFormat("yyyy-MM-dd hh: mm aa"). form(Calendar.getInstance(). getTime()); /Calendarc=Calendar.getInstance(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh: mm: ss aa"); String formattedDate=sdf.format(c.getTime());/ /*TimeZone utc=TimeZone.getTimeZone("etc/UTC"); DateFormat inputFormat=new SimpleDateFormat("dd MMM, yyyy HH: mm", Locale. US); inputFormat.setTimeZone(utc); DateFormat outputFormat=new SimpleDateFormat("dd MMM, yyyy hh: mm aa", Locale. US); outputFormat.setTimeZone(utc);
Date date = inputFormat.parse(input);
String output = outputFormat.format(date);*/
// fabBtn.setVisibility(View.VISIBLE);
//currentDateTimeString = java.text.DateFormat.getDateTimeInstance().format(new java.util.Date());
duration_text_view.setText("\nPresent DATE/TIME: " + currentDateTimeString + "\nDevice DATE/TIME: " + deviceTime + "\nLOCATION: " + distance + " from your location point.");
} else {
String snackbar_data = null;
snackbar_data = "Date/Time:" + deviceTime + "\nLocation:" + distance + " from your location point.";
View parentLayout = findViewById(R.id.map);
Snackbar snackbar = Snackbar
.make(parentLayout, snackbar_data, Snackbar.LENGTH_INDEFINITE)
.setAction("CLOSE", new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
//更改消息文本颜色snackbar。setActionTextColor(颜色:红色);
//更改操作按钮文本颜色View sbView=snackbar.getView(); TextView text View=(TextView)sbView.findViewById(android.support.design.R.id.snackbar_text); text View.setTextColor(Color. WHITE); text View.set行(5);snackbar.show();
}
} catch (Exception e) {
}
}
} else {
Toast.makeText(getcontext, "Check net connection", Toast.LENGTH_LONG).show();
}
}
}
试试用这个:http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/SphericalUtil.html
对于您获得的每个位置,您都创建一个LatLng并将其添加到ArrayList中。然后使用此函数,它会为您提供正确的长度:
SphericalUtil.computeLength(latLngs);
Gradle:
dependencies {
compile 'com.google.maps.android:android-maps-utils:0.4+'
}
编辑:您有一个全局Arraylist locations=new Arraylist。在“onLocationChanged”中,您将添加到位置:
@Override
public void onLocationChanged(Location location) {
locations.add(location);
}
然后在需要的时候你做:
for (Location loc : locations){
latLngs.add(new LatLng(loc.getLocation().getLat(), loc.getLocation().getLng()));
}
Double dist = SphericalUtil.computeLength(latLngs);
我需要计算汽车行驶的距离!不是距离,不是距离到否。如果我们通过谷歌提供的API计算,距离可以完全不同。谷歌可以提供从一个点到另一个点的1公里距离,但汽车可以按照骑手想要的方式行驶800米。使用加速计没有帮助。它适用于步行,但绝不适用于更快的速度。 我尝试过使用Google的位置API:距离到或距离之间根本不是一个选项。它可以给出与IN REAL截然不同的结果。在真实的汽车中,可以通过非常短的地方并
问题内容: 如何在Swift中使用CoreLocation计算行进的总距离 到目前为止,我还无法找到有关如何在iOS 8的Swift中执行此操作的任何资源。 自开始跟踪位置以来,您将如何计算移动的总距离? 根据到目前为止的读物,我需要保存一个点的位置,然后计算当前点与最后一个点之间的距离,然后将该距离添加到totalDistance变量中 Objective-C对我来说是非常陌生的,所以我还无法计
我正在尝试写我的第一次postgis查询 我的桌子如下所示 这是一辆id为1的车辆的gps数据。我需要计算车辆行驶的总距离,比方说2017-05-20年,以米为单位。可以有其他具有不同ID的视图。 参考(如)。https://gis.stackexchange.com/Questions/268776/Finding-Total-Distance of-Path-Along-Post-PointG
问题内容: 我在用 python 2.7.12 Django 1.10.6 PostgreSQL 9.5.6 postGIS 2.2.2 第一个问题 我需要使用GeoDjango计算两点之间的距离。当我检查了 文档它说, GeoQuerySet.distance() 已被弃用,而使用 距离() 从 django.contrib.gis.db.models.functions 。 以下代码可以正常工
我有一个多边形类型的几何体,我正在计算一个点的最小距离可能在多边形几何体内部(由360个点组成,作为闭合几何体)或多边形几何体外部。使用postgis的ST_distance方法,当点在几何体外部时,我得到精确的距离,但如果点在几何体内部,则得到0作为距离,我想要与多边形几何体最近点的点之间的最小距离,无论该点位于几何体内部还是外部。
我使用行人库(使用ps源、ping Goto和ps汇),想要模拟人行道环境。这个模型的目标是得到行人之间的距离小于1m的数据。所以,我尝试计算行人之间的距离。在Any逻辑中,可以通过使用getX()、getY和getId(可以每秒计算)来收集行人的信息。但是我不知道如何选择行人代理并计算它们之间的距离。我的意思是,如果有10个行人(id:1, 2, 3...),如何每秒钟获得1和2、1和3、2和3