App distance traveled on trail trace.
This commit is contained in:
parent
f7d719276b
commit
259f12ac3f
|
|
@ -22,6 +22,7 @@ import android.view.MenuItem;
|
|||
import android.view.Surface;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.osmdroid.api.IMapController;
|
||||
|
|
@ -71,6 +72,10 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
private FloatingActionButton start_stop_trace;
|
||||
private FloatingActionButton start_stop_dog_trace;
|
||||
private FloatingActionButton add_object;
|
||||
private TextView textViewCurrentLocation;
|
||||
|
||||
private MyLocation lastLocation = null;
|
||||
private int distance = 0;
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
protected void checkPermissions() {
|
||||
|
|
@ -148,6 +153,9 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
}
|
||||
});
|
||||
|
||||
textViewCurrentLocation = findViewById(R.id.textViewCurrentLocation);
|
||||
textViewCurrentLocation.setVisibility(View.GONE);
|
||||
|
||||
if (serviceGps == null) {
|
||||
serviceGps = new ServiceGps();
|
||||
serviceGps.addObserver(this);
|
||||
|
|
@ -160,6 +168,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
this.start_stop_trace.setImageResource(R.drawable.ic_stop);
|
||||
this.add_object.setVisibility(View.VISIBLE);
|
||||
this.start_stop_dog_trace.setVisibility(View.GONE);
|
||||
this.textViewCurrentLocation.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
this.start_stop_trace.setImageResource(R.drawable.ic_play);
|
||||
this.add_object.setVisibility(View.GONE);
|
||||
|
|
@ -315,6 +324,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
serviceGps.importGpxTrace(new File(data.getStringExtra(FilePicker.EXTRA_FILE_PATH)));
|
||||
updateDogTrace();
|
||||
updateTrailTrace();
|
||||
calculTrailDistance();
|
||||
for (MyLocation loc: serviceGps.getListGeoPointObjects()) {
|
||||
addMarker(new GeoPoint(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()));
|
||||
}
|
||||
|
|
@ -384,6 +394,21 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
}
|
||||
}
|
||||
|
||||
private void calculTrailDistance() {
|
||||
List<MyLocation> listLoc = serviceGps.getListGeoPointTraceur();
|
||||
MyLocation last = null;
|
||||
int distance = 0;
|
||||
for (MyLocation loc: listLoc) {
|
||||
if (last != null) {
|
||||
distance += loc.distanceTo(last);
|
||||
}
|
||||
last = loc;
|
||||
}
|
||||
if (distance != 0) {
|
||||
updateDistance(distance);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateDogTrace() {
|
||||
Polyline line = new Polyline(map);
|
||||
line.setTitle(Gpx.DOG_TRACE_NAME);
|
||||
|
|
@ -418,6 +443,11 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
map.getController().setCenter(currentPoint);
|
||||
if (serviceGps.isTraceurActivated()) {
|
||||
updateTrailTrace();
|
||||
if (lastLocation != null) {
|
||||
distance += loc.distanceTo(lastLocation);
|
||||
updateDistance(distance);
|
||||
}
|
||||
lastLocation = loc;
|
||||
} else if (serviceGps.isDogActivated()) {
|
||||
updateDogTrace();
|
||||
}
|
||||
|
|
@ -440,4 +470,9 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu
|
|||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private void updateDistance(int distance) {
|
||||
this.textViewCurrentLocation.setText("Distance: "+distance+"m");
|
||||
this.textViewCurrentLocation.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,6 +68,19 @@ public class MyLocation {
|
|||
return this.bearing;
|
||||
}
|
||||
|
||||
public int distanceTo(MyLocation loc) {
|
||||
double earthRadius = 6371000; //meters
|
||||
double dLat = Math.toRadians(loc.latitude-latitude);
|
||||
double dLng = Math.toRadians(loc.longitude-longitude);
|
||||
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
|
||||
Math.cos(Math.toRadians(latitude)) * Math.cos(Math.toRadians(loc.latitude)) *
|
||||
Math.sin(dLng/2) * Math.sin(dLng/2);
|
||||
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
|
||||
float dist = (float) (earthRadius * c);
|
||||
|
||||
return (int) dist;
|
||||
}
|
||||
|
||||
|
||||
public String toWayPoint() {
|
||||
String ret = "<wpt lat=\""+getLatitude()+"\" lon=\""+getLongitude()+"\">";
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
|
|
@ -7,7 +6,6 @@
|
|||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
|
||||
<android.support.design.widget.AppBarLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
|
|
@ -20,6 +18,14 @@
|
|||
android:background="?attr/colorPrimary"
|
||||
app:popupTheme="@style/AppTheme.PopupOverlay" />
|
||||
|
||||
<TextView android:id="@+id/textViewCurrentLocation"
|
||||
android:layout_width="fill_parent"
|
||||
android:text="PLACEHOLDER"
|
||||
android:textAlignment="center"
|
||||
android:gravity="center"
|
||||
android:textColor="@android:color/white"
|
||||
android:background="#77000000"
|
||||
android:layout_height="wrap_content"></TextView>
|
||||
</android.support.design.widget.AppBarLayout>
|
||||
|
||||
<org.osmdroid.views.MapView android:id="@+id/map"
|
||||
|
|
|
|||
Loading…
Reference in New Issue