From e8af38981f40af4f5d528613e77073c00a4818a1 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 5 Jan 2021 11:25:13 +0100 Subject: [PATCH] added visualizing route --- .../a1/nextlocation/data/CurrentRoute.java | 18 ++++++ .../com/a1/nextlocation/data/Location.java | 2 +- .../nextlocation/fragments/HomeFragment.java | 64 ++++++++++++++++--- .../nextlocation/fragments/RouteFragment.java | 22 +++---- .../nextlocation/json/DirectionsResult.java | 40 ++++++++++-- .../a1/nextlocation/network/ApiHandler.java | 18 +++++- app/src/main/res/values/colors.xml | 1 + 7 files changed, 134 insertions(+), 31 deletions(-) create mode 100644 app/src/main/java/com/a1/nextlocation/data/CurrentRoute.java diff --git a/app/src/main/java/com/a1/nextlocation/data/CurrentRoute.java b/app/src/main/java/com/a1/nextlocation/data/CurrentRoute.java new file mode 100644 index 0000000..a0642f0 --- /dev/null +++ b/app/src/main/java/com/a1/nextlocation/data/CurrentRoute.java @@ -0,0 +1,18 @@ +package com.a1.nextlocation.data; + +import org.osmdroid.views.overlay.Polyline; + +public enum CurrentRoute { + INSTANCE; + + private Polyline currentRoute; + + public void setCurrentRoute(Polyline currentRoute) { + this.currentRoute = currentRoute; + } + + public Polyline getCurrentRoute() { + return currentRoute; + } + +} diff --git a/app/src/main/java/com/a1/nextlocation/data/Location.java b/app/src/main/java/com/a1/nextlocation/data/Location.java index f7d2362..e0eb89d 100644 --- a/app/src/main/java/com/a1/nextlocation/data/Location.java +++ b/app/src/main/java/com/a1/nextlocation/data/Location.java @@ -105,7 +105,7 @@ public class Location implements Parcelable { } public static String getStringFromCoordinates(double lat1, double long1) { - return lat1 + "," + long1; + return long1 + "," + lat1; } public GeoPoint convertToGeoPoint() { diff --git a/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java index 621d02b..e6dcd75 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java @@ -23,31 +23,45 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import com.a1.nextlocation.R; +import com.a1.nextlocation.data.CurrentRoute; import com.a1.nextlocation.data.Route; +import com.a1.nextlocation.json.DirectionsResult; +import com.a1.nextlocation.network.ApiHandler; import com.a1.nextlocation.recyclerview.CouponListManager; import com.a1.nextlocation.recyclerview.CustomOverlay; import com.a1.nextlocation.recyclerview.LocationListManager; import com.a1.nextlocation.recyclerview.RouteListManager; import org.osmdroid.api.IMapController; +import org.osmdroid.bonuspack.routing.MapQuestRoadManager; +import org.osmdroid.bonuspack.routing.OSRMRoadManager; +import org.osmdroid.bonuspack.routing.Road; +import org.osmdroid.bonuspack.routing.RoadManager; import org.osmdroid.config.Configuration; import org.osmdroid.util.GeoPoint; import org.osmdroid.views.MapView; import org.osmdroid.views.overlay.Overlay; import org.osmdroid.views.overlay.OverlayItem; +import org.osmdroid.views.overlay.Polyline; import org.osmdroid.views.overlay.compass.CompassOverlay; import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider; import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider; import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; public class HomeFragment extends Fragment { private final String userAgent = "com.ai.nextlocation.fragments"; + public final static String MAPQUEST_API_KEY = "vuyXjqnAADpjeL9QwtgWGleIk95e36My"; private ImageButton imageButton; private MapView mapView; private final int REQUEST_PERMISSIONS_REQUEST_CODE = 1; private final String TAG = HomeFragment.class.getCanonicalName(); + private RoadManager roadManager; + private Polyline roadOverlay; @Override public void onCreate(Bundle savedInstanceState) { @@ -57,6 +71,7 @@ public class HomeFragment extends Fragment { Manifest.permission.ACCESS_FINE_LOCATION, // WRITE_EXTERNAL_STORAGE is required in order to show the map Manifest.permission.WRITE_EXTERNAL_STORAGE); + roadManager = new OSRMRoadManager(requireContext()); } @@ -72,9 +87,25 @@ public class HomeFragment extends Fragment { ((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, locationFragment).addToBackStack(null).commit(); }); + +// roadManager.addRequestOption("routeType=foot-walking"); + ApiHandler.INSTANCE.addListener(this::onDirectionsAvailable); return view; } + private void onDirectionsAvailable(DirectionsResult directionsResult) { + Log.d(TAG, "onDirectionsAvailable: got result! " + directionsResult); + ArrayList geoPoints = directionsResult.getGeoPoints(); + roadOverlay = new Polyline(); + roadOverlay.setPoints(geoPoints); + roadOverlay.setColor(R.color.red); + + + CurrentRoute.INSTANCE.setCurrentRoute(roadOverlay); + Log.d(TAG, "onDirectionsAvailable: successfully added road!"); + + } + @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); @@ -106,18 +137,18 @@ public class HomeFragment extends Fragment { mLocationOverlay.enableMyLocation(); mapView.getOverlays().add(mLocationOverlay); - CustomOverlay customOverlay = new CustomOverlay(getResources().getDrawable(R.drawable.ic_baseline_location_on_24),mapView); - - for (com.a1.nextlocation.data.Location l : LocationListManager.INSTANCE.getLocationList()) { - GeoPoint p = new GeoPoint(l.getLat(), l.getLong()); - OverlayItem overlayItem = new OverlayItem(l.getName(),l.getDescription(), p); - - customOverlay.addOverlayItem(overlayItem); - Log.d(TAG, "initMap: " + "succes"); - } +// CustomOverlay customOverlay = new CustomOverlay(getResources().getDrawable(R.drawable.ic_baseline_location_on_24),mapView); +// +// for (com.a1.nextlocation.data.Location l : LocationListManager.INSTANCE.getLocationList()) { +// GeoPoint p = new GeoPoint(l.getLat(), l.getLong()); +// OverlayItem overlayItem = new OverlayItem(l.getName(),l.getDescription(), p); +// +// customOverlay.addOverlayItem(overlayItem); +// Log.d(TAG, "initMap: " + "succes"); +// } - mapView.getOverlays().add(customOverlay); +// mapView.getOverlays().add(customOverlay); // add the zoom controller IMapController mapController = mapView.getController(); @@ -143,6 +174,19 @@ public class HomeFragment extends Fragment { } + if (roadOverlay == null) { + if (CurrentRoute.INSTANCE.getCurrentRoute() != null) { + roadOverlay = CurrentRoute.INSTANCE.getCurrentRoute(); + mapView.getOverlays().add(roadOverlay); + mapView.invalidate(); + Log.d(TAG, "initMap: successfully added road!"); + } + } else { + mapView.getOverlays().add(roadOverlay); + mapView.invalidate(); + Log.d(TAG, "initMap: successfully added road!"); + } + } private void requestPermissionsIfNecessary(String... permissions) { ArrayList permissionsToRequest = new ArrayList<>(); diff --git a/app/src/main/java/com/a1/nextlocation/fragments/RouteFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/RouteFragment.java index 752b7d6..85d8c02 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/RouteFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/RouteFragment.java @@ -38,7 +38,6 @@ public class RouteFragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - ApiHandler.INSTANCE.addListener(this::onDirectionsAvailable); } @@ -71,16 +70,15 @@ public class RouteFragment extends Fragment { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); -// ApiHandler.INSTANCE.getDirections(8.681436,49.41461,8.687872,49.420318); -// Route r = new Route("test"); -// r.addLocation(new Location("test",8.681436,49.41461,"route",null)); -// r.addLocation(new Location("test",8.687872,49.420318,"route",null)); -// ApiHandler.INSTANCE.getDirections(r); - } - - public void onDirectionsAvailable(DirectionsResult result) { - Log.d(TAG, "onDirectionsAvailable: got result! " + result); - - + ApiHandler.INSTANCE.getDirections(51.49017262451581, 4.289038164073164,51.47337383133509, 4.303535222390562); + Route r = new Route("test"); + r.addLocation(new Location("test",51.487963606716185, 4.280396603442448,"route",null)); + r.addLocation(new Location("test",51.486962126272886, 4.27884897122147,"route",null)); + r.addLocation(new Location("test",51.485412622620224, 4.277392376189963,"route",null)); + r.addLocation(new Location("test",51.4801212376542, 4.281610432511638,"route",null)); + r.addLocation(new Location("test",51.481103969835665, 4.2903500027691805,"route",null)); +// r.addLocation(new Location("test",51.489063681658145, 4.289596063527951,"route",null)); +// r.addLocation(new Location("test",51.483012677667766, 4.28003245468457,"route",null)); + ApiHandler.INSTANCE.getDirections(r); } } \ No newline at end of file diff --git a/app/src/main/java/com/a1/nextlocation/json/DirectionsResult.java b/app/src/main/java/com/a1/nextlocation/json/DirectionsResult.java index 8cf1c7c..7f52783 100644 --- a/app/src/main/java/com/a1/nextlocation/json/DirectionsResult.java +++ b/app/src/main/java/com/a1/nextlocation/json/DirectionsResult.java @@ -15,6 +15,7 @@ import org.osmdroid.util.GeoPoint; import java.lang.reflect.Array; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class DirectionsResult { @@ -23,6 +24,7 @@ public class DirectionsResult { private double distance; private double duration; private double[][] wayPointCoordinates; + private GeoPoint[] startAndEndPoint = new GeoPoint[2]; public List getSteps() { return steps; @@ -52,8 +54,31 @@ public class DirectionsResult { this.steps.add(step); } + public GeoPoint[] getStartAndEndPoint() { + return startAndEndPoint; + } + + /** + * converts all the geopoints in all the steps into an arraylist to display it on the map + * @return the list of geopoints + */ + public ArrayList getGeoPoints() { + int size = 0; + // we'll have a lot of waypoints, so calculate the size first so that the list won't have to be extended (o p t i m i z e) + for (int i = 0; i < this.getSteps().size(); i++) { + size += this.getSteps().get(i).getWaypoints().length; + } + + ArrayList res = new ArrayList<>(size); + for (DirectionsStep step : this.getSteps()) { + Collections.addAll(res, step.getWaypoints()); + } + return res; + } + /** * parses a given json string into this object. It gets all the waypoints and steps and combines them so that every step also has the correct coordinates associated with it + * * @param json the json string to parse. */ public void parse(String json) { @@ -86,7 +111,7 @@ public class DirectionsResult { for (JsonElement j : steps) { - DirectionsStep step = gson.fromJson(j,DirectionsStep.class); + DirectionsStep step = gson.fromJson(j, DirectionsStep.class); double lat; double longl; @@ -94,7 +119,7 @@ public class DirectionsResult { for (int i = 0; i < 2; i++) { lat = this.wayPointCoordinates[step.getWay_points().get(i)][0]; longl = this.wayPointCoordinates[step.getWay_points().get(i)][1]; - step.getWaypoints()[i] = new GeoPoint(lat,longl); + step.getWaypoints()[i] = new GeoPoint(lat, longl); } addStep(step); @@ -102,6 +127,9 @@ public class DirectionsResult { } } + startAndEndPoint[0] = this.getSteps().get(0).getWaypoints()[0]; + startAndEndPoint[1] = this.getSteps().get(this.getSteps().size()-1).getWaypoints()[1]; + } public void parseRoute(String json) { @@ -115,7 +143,7 @@ public class DirectionsResult { this.duration = summary.get("duration").getAsDouble(); JsonPrimitive geometry = route.getAsJsonPrimitive("geometry"); - JsonArray wayPointCoordinates = GeometryDecoder.decodeGeometry(geometry.getAsString(),false); + JsonArray wayPointCoordinates = GeometryDecoder.decodeGeometry(geometry.getAsString(), false); this.wayPointCoordinates = new double[wayPointCoordinates.size()][2]; @@ -140,7 +168,7 @@ public class DirectionsResult { for (JsonElement j : steps) { - DirectionsStep step = gson.fromJson(j,DirectionsStep.class); + DirectionsStep step = gson.fromJson(j, DirectionsStep.class); double lat; double longl; @@ -148,7 +176,7 @@ public class DirectionsResult { for (int i = 0; i < 2; i++) { lat = this.wayPointCoordinates[step.getWay_points().get(i)][0]; longl = this.wayPointCoordinates[step.getWay_points().get(i)][1]; - step.getWaypoints()[i] = new GeoPoint(lat,longl); + step.getWaypoints()[i] = new GeoPoint(lat, longl); } addStep(step); @@ -158,7 +186,5 @@ public class DirectionsResult { } - - } } diff --git a/app/src/main/java/com/a1/nextlocation/network/ApiHandler.java b/app/src/main/java/com/a1/nextlocation/network/ApiHandler.java index 1b480a8..5787b30 100644 --- a/app/src/main/java/com/a1/nextlocation/network/ApiHandler.java +++ b/app/src/main/java/com/a1/nextlocation/network/ApiHandler.java @@ -38,7 +38,7 @@ public enum ApiHandler { } public void getDirections(double startLat, double startLong, double endLat, double endLong) { - getDirections(startLat + "," + startLong, endLat + "," + endLong); + getDirections(startLong + "," + startLat, endLong + "," + endLat); } public void getDirections(String startLocation, String endLocation) { @@ -69,6 +69,12 @@ public enum ApiHandler { t.start(); +// try { +// t.join(); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } + } public void addListener(DirectionsListener listener) { @@ -104,6 +110,10 @@ public enum ApiHandler { if (response.body() != null) { String responseString = Objects.requireNonNull(response.body()).string(); Log.d(TAG, "getDirections: got response: " + responseString); + if (responseString.startsWith("{\"error")) { + Log.e(TAG, "getDirections: ERROR IN REQUEST!"); + return; + } DirectionsResult result = new DirectionsResult(); result.parseRoute(responseString); @@ -121,6 +131,12 @@ public enum ApiHandler { t.start(); +// try { +// t.join(); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } + } diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index cf6f6b1..a8f6080 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -10,4 +10,5 @@ #FF115571 #FF31AFB4 #FF14212D + #FF0000 \ No newline at end of file