Merge branch 'follow-route' of https://github.com/SemvdH/Next-Location into follow-route

This commit is contained in:
Sem van der Hoeven
2021-01-06 16:57:08 +01:00
9 changed files with 131 additions and 43 deletions

View File

@@ -112,15 +112,6 @@ public class Location implements Parcelable {
return long1 + "," + lat1;
}
/**
* calculates the distance to the other location.
* @param other the other location
* @return the distance between the locations in meters
*/
public double getDistance(Location other) {
return getDistance(this.getLat(),this.getLong(),other.getLat(),other.getLong());
}
/**
* calculates the distance between two coordinates
* @param lat1 the first latitude
@@ -145,8 +136,12 @@ public class Location implements Parcelable {
// Radius of earth in kilometers. Use 3956
// for miles
double r = 6371;
// if(miles) {
// double r = 3956;
// }
// else {
double r = 6371;
// }
// calculate the result
double distance = c * r;

View File

@@ -66,7 +66,6 @@ public class Route implements Parcelable {
}
public float getTotalDistance() {
//TODO calculate total distance according to all locations in list
return totalDistance;
}

View File

@@ -28,6 +28,10 @@ public enum RouteHandler {
this.routeFinishedListener = routeFinishedListener;
}
public RouteFinishedListener getRouteFinishedListener() {
return routeFinishedListener;
}
public int getStepCount() {
return stepCount;
}

View File

@@ -1,5 +1,6 @@
package com.a1.nextlocation.fragments;
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
@@ -27,6 +28,7 @@ public class RouteDetailFragment extends Fragment {
private Route route;
private TextView routeDetailText;
private TextView routeName;
private TextView totalDistance;
private ImageButton imageButton;
@Override
@@ -35,6 +37,7 @@ public class RouteDetailFragment extends Fragment {
}
@SuppressLint("SetTextI18n")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_route_detail, container, false);
@@ -48,6 +51,10 @@ public class RouteDetailFragment extends Fragment {
this.routeDetailText = view.findViewById(R.id.reoute_detail_tekst);
this.routeDetailText.setText(this.route.getDescription());
this.totalDistance = view.findViewById(R.id.total_distance);
String distance_tekst = getResources().getString(R.string.total_distance_route);
this.totalDistance.setText(distance_tekst + " " + calculateRoute(this.route.getLocations()) + "m");
this.imageButton = view.findViewById(R.id.route_detail_back_button);
this.imageButton.setOnClickListener(v -> {
RouteFragment routeFragment = new RouteFragment();
@@ -57,8 +64,6 @@ public class RouteDetailFragment extends Fragment {
Button startButton = view.findViewById(R.id.start_route_button);
startButton.setOnClickListener(this::startRoute);
calculateRoute(this.route.getLocations());
return view;
}
@@ -71,24 +76,28 @@ public class RouteDetailFragment extends Fragment {
RouteHandler.INSTANCE.followRoute(route);
Toast.makeText(requireContext(),"Route started!",Toast.LENGTH_SHORT).show();
((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, new HomeFragment()).addToBackStack(null).commit();
}
//Calculates the route total distance
/**
* Calculates the distance between points
* @param route the route that is calculated
* @return the total distance of a route
*/
public double calculateRoute(List<Location> route){
ArrayList<Location> routeArraylist = new ArrayList<>(route);
double totalDistance = 0;
Location firstLocation;
Location secondLocation;
System.out.println("Total locations: " + routeArraylist.size());
//Cycles through the arraylist
for(int i = 0; i < routeArraylist.size() - 1; i++) {
firstLocation = routeArraylist.get(i);
secondLocation = routeArraylist.get(i+1);
System.out.println("locations distance calculated: " + (i+1) + " and " + (i+2) + "\nThe added distance is: " + Location.getDistance(firstLocation.getLat(), firstLocation.getLong(), secondLocation.getLat(), secondLocation.getLong()));
//Calculates the distance between points
totalDistance += Location.getDistance(firstLocation.getLat(), firstLocation.getLong(), secondLocation.getLat(), secondLocation.getLong());
// totalDistance += firstLocation.getDistance(secondLocation);
System.out.println("Distance nu: " + totalDistance);
}
System.out.println("Total Distance: " + totalDistance);
return totalDistance;
}