Distance Calculation works, added distance test
This commit is contained in:
@@ -4,37 +4,74 @@
|
||||
"locations": [
|
||||
{
|
||||
"name":"Prison Escape Kloosterlaan 168",
|
||||
"coordinates":"51.59073795635181,4.784917104321059",
|
||||
"coordinates":"51.59058740778487, 4.784723144974098",
|
||||
"description":"4811EE Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"De Koepel - FutureDome Events Nassausingel 26",
|
||||
"coordinates":"51.590431588532105,4.786756741648511",
|
||||
"coordinates":"51.59025697138579, 4.787354025225596",
|
||||
"description":"4811HP Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
|
||||
{
|
||||
"name":"Escaping Breda: Escape Room Games Boschstraat 114",
|
||||
"coordinates":" 51.59110835530862,4.784147222780912",
|
||||
"coordinates":"51.59096518753322, 4.783727736123234",
|
||||
"description":"4811GK Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"MEZZ Breda Keizerstraat 101",
|
||||
"coordinates":"51.58394697737321,4.779757901349616",
|
||||
"coordinates":"51.5837840024532, 4.779037836841554",
|
||||
"description":"4811HL Breda",
|
||||
"imageUrl":"NULL"
|
||||
},
|
||||
{
|
||||
"name":"Het Klooster Breda Schorsmolenstraat 13",
|
||||
"coordinates":"51.58775443759389,4.765568874365066",
|
||||
"coordinates":"51.58765659148822, 4.764801414019652",
|
||||
"description":"4811VN Breda",
|
||||
"imageUrl":"NULL"
|
||||
}
|
||||
],
|
||||
"totalDistance": 2.3434,
|
||||
"totalTime": 342342
|
||||
},
|
||||
{
|
||||
"name": "Obesi Route",
|
||||
"locations": [
|
||||
{
|
||||
"name": "KesCrOkÊt breeda",
|
||||
"coordinates": "2.4654645,6.2342323",
|
||||
"description": "lekkere patatjes"
|
||||
},
|
||||
{
|
||||
"name": "McDonald's Breda Karnemelkstraat",
|
||||
"coordinates": "51.58596318905295,4.77586651481887",
|
||||
"description": "Wist je dat McDonald’s de grootste restaurantketen ter wereld is? Hierdoor hebben we een indrukwekkende geschiedenis. Maar wij kijken liever naar onze toekomst. Nieuwe restaurants, betere producten en duurzaam ondernemen. Wil je meer weten over McDonald’s? Lees verder op: https://www.mcdonalds.com/nl/nl-nl/over-ons.html!"
|
||||
},
|
||||
{
|
||||
"name": "Wok To Go Halstraat 40",
|
||||
"coordinates": "51.58650312572897,4.777467069592386",
|
||||
"description": "Bij Wok To Go houden we van snelle, gezonde en smaakvolle maaltijden. Sinds 2003 ontwikkelen onze chef-koks de lekkerste wokgerechten met verse ingrediënten. We doen dit vól overgave onder het motto “Making people happy through food”. Door het roerbakken – met een kleine hoeveelheid olie – op grote hitte, blijven de smaak en gezonde voedingsstoffen bewaard. We kiezen voor gezonde en unieke ingrediënten en bereiden uitgebalanceerde gerechten in een ‘Wokki box’ voor een gezonde levensstijl."
|
||||
},
|
||||
{
|
||||
"name": "De Boterhal Grote Markt 19",
|
||||
"coordinates": "51.588069667604756,4.7762685632180695",
|
||||
"description": "De Boterhal in Breda is een internationaal tapasrestaurant, speciaalbiercafé en wijnbar. Men kan gezellig bij ons lunchen, borrelen en dineren. Wij hebben een uitgebreide menukaart met een hele hoop bieren, wijnen, zowel per glas als per fles te verkrijgen, lunch en tapasgerechtjes. Naast het eten wat wij serveren hebben we ook een hoop bieren en wijnen. Naast de bieren op de kaart hebben wij wisselende bieren; vijf ketelbieren en vijf seizoensbieren."
|
||||
},
|
||||
{
|
||||
"name": "Gauchos Grote Markt 33",
|
||||
"coordinates": "51.58859328857082,4.77601349563689",
|
||||
"description": "De middeleeuwse Grote Markt is een historische plek in het Bourgondische hart van Breda, ooit in handen van de Spaanse bezetter. Gauchos Grill-Restaurant combineert daar Brabantse gemoedelijkheid met professionele gastvrijheid. In onze Latijnse keuken bereiden wij op uw aanwijzing een malse Argentijnse steak of een smakelijk visgerecht van de grill."
|
||||
},
|
||||
{
|
||||
"name": "The Tosti Club Breda A4, Vlaszak 2",
|
||||
"coordinates": "51.58905632735254,4.780729463841719",
|
||||
"description": "Je kunt bij al onze vestigingen de hele dag terecht, want bij The Tosti Club eet je het klokje rond. Dus kom gezellig ontbijten, lunchen of voor een snelle hap aan het einde van de dag (wat dacht je van onze The Tosti Slice, onze variant op pizza!). Even geen zin in brood? Dan kunnen we je ook verblijden met onder andere acaï smoothiebowls, pancakes, granola’s / yoghurt of gewoon een lekkere warme drank met gebak. Mogen we je de Red Velvet fudge cake aanraden?"
|
||||
}
|
||||
],
|
||||
"totalDistance": 2.3434,
|
||||
"totalTime": 342342
|
||||
}
|
||||
]
|
||||
@@ -130,6 +130,11 @@ public class Location implements Parcelable {
|
||||
* @return the distance between the coordinates in meters
|
||||
*/
|
||||
public static double getDistance(double lat1, double lon1, double lat2, double lon2) {
|
||||
lat1 = Math.toRadians(lat1);
|
||||
lat2 = Math.toRadians(lat2);
|
||||
lon1 = Math.toRadians(lon1);
|
||||
lon2 = Math.toRadians(lon2);
|
||||
|
||||
double dlon = lon2 - lon1;
|
||||
double dlat = lat2 - lat1;
|
||||
double a = Math.pow(Math.sin(dlat / 2), 2)
|
||||
@@ -145,6 +150,7 @@ public class Location implements Parcelable {
|
||||
// calculate the result
|
||||
double distance = c * r;
|
||||
|
||||
distance *= 1000;
|
||||
return Math.floor(distance);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,10 +14,14 @@ import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.a1.nextlocation.R;
|
||||
import com.a1.nextlocation.data.Location;
|
||||
import com.a1.nextlocation.data.Route;
|
||||
import com.a1.nextlocation.data.RouteHandler;
|
||||
import com.a1.nextlocation.network.ApiHandler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class RouteDetailFragment extends Fragment {
|
||||
|
||||
private Route route;
|
||||
@@ -53,6 +57,7 @@ public class RouteDetailFragment extends Fragment {
|
||||
Button startButton = view.findViewById(R.id.start_route_button);
|
||||
startButton.setOnClickListener(this::startRoute);
|
||||
|
||||
calculateRoute(this.route.getLocations());
|
||||
|
||||
return view;
|
||||
}
|
||||
@@ -68,4 +73,23 @@ public class RouteDetailFragment extends Fragment {
|
||||
((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, new HomeFragment()).addToBackStack(null).commit();
|
||||
|
||||
}
|
||||
|
||||
//Calculates the route total distance
|
||||
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());
|
||||
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()));
|
||||
totalDistance += Location.getDistance(firstLocation.getLat(), firstLocation.getLong(), secondLocation.getLat(), secondLocation.getLong());
|
||||
// totalDistance += firstLocation.getDistance(secondLocation);
|
||||
System.out.println("Distance nu: " + totalDistance);
|
||||
}
|
||||
return totalDistance;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.a1.nextlocation;
|
||||
|
||||
import android.graphics.Point;
|
||||
|
||||
import com.a1.nextlocation.data.Location;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -94,4 +96,13 @@ public class LocationTest {
|
||||
|
||||
assertNotNull(alternateTestLocation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void calculatingDistance(){
|
||||
|
||||
double expected2 = 1054.66;
|
||||
assertEquals(expected2, Location.getDistance(51.578810244278344, 4.804990650154528, 51.57696163247576, 4.789331956851824), 100);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user