added visualizing route

This commit is contained in:
Sem van der Hoeven
2021-01-05 11:25:13 +01:00
parent bcd9eb6378
commit e8af38981f
7 changed files with 134 additions and 31 deletions

View File

@@ -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;
}
}

View File

@@ -105,7 +105,7 @@ public class Location implements Parcelable {
} }
public static String getStringFromCoordinates(double lat1, double long1) { public static String getStringFromCoordinates(double lat1, double long1) {
return lat1 + "," + long1; return long1 + "," + lat1;
} }
public GeoPoint convertToGeoPoint() { public GeoPoint convertToGeoPoint() {

View File

@@ -23,31 +23,45 @@ import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import com.a1.nextlocation.R; import com.a1.nextlocation.R;
import com.a1.nextlocation.data.CurrentRoute;
import com.a1.nextlocation.data.Route; 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.CouponListManager;
import com.a1.nextlocation.recyclerview.CustomOverlay; import com.a1.nextlocation.recyclerview.CustomOverlay;
import com.a1.nextlocation.recyclerview.LocationListManager; import com.a1.nextlocation.recyclerview.LocationListManager;
import com.a1.nextlocation.recyclerview.RouteListManager; import com.a1.nextlocation.recyclerview.RouteListManager;
import org.osmdroid.api.IMapController; 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.config.Configuration;
import org.osmdroid.util.GeoPoint; import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView; import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Overlay; import org.osmdroid.views.overlay.Overlay;
import org.osmdroid.views.overlay.OverlayItem; 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.CompassOverlay;
import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider; import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider;
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider; import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider;
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay; import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class HomeFragment extends Fragment { public class HomeFragment extends Fragment {
private final String userAgent = "com.ai.nextlocation.fragments"; private final String userAgent = "com.ai.nextlocation.fragments";
public final static String MAPQUEST_API_KEY = "vuyXjqnAADpjeL9QwtgWGleIk95e36My";
private ImageButton imageButton; private ImageButton imageButton;
private MapView mapView; private MapView mapView;
private final int REQUEST_PERMISSIONS_REQUEST_CODE = 1; private final int REQUEST_PERMISSIONS_REQUEST_CODE = 1;
private final String TAG = HomeFragment.class.getCanonicalName(); private final String TAG = HomeFragment.class.getCanonicalName();
private RoadManager roadManager;
private Polyline roadOverlay;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@@ -57,6 +71,7 @@ public class HomeFragment extends Fragment {
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION,
// WRITE_EXTERNAL_STORAGE is required in order to show the map // WRITE_EXTERNAL_STORAGE is required in order to show the map
Manifest.permission.WRITE_EXTERNAL_STORAGE); 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(); ((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; return view;
} }
private void onDirectionsAvailable(DirectionsResult directionsResult) {
Log.d(TAG, "onDirectionsAvailable: got result! " + directionsResult);
ArrayList<GeoPoint> 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 @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
@@ -106,18 +137,18 @@ public class HomeFragment extends Fragment {
mLocationOverlay.enableMyLocation(); mLocationOverlay.enableMyLocation();
mapView.getOverlays().add(mLocationOverlay); mapView.getOverlays().add(mLocationOverlay);
CustomOverlay customOverlay = new CustomOverlay(getResources().getDrawable(R.drawable.ic_baseline_location_on_24),mapView); // CustomOverlay customOverlay = new CustomOverlay(getResources().getDrawable(R.drawable.ic_baseline_location_on_24),mapView);
//
for (com.a1.nextlocation.data.Location l : LocationListManager.INSTANCE.getLocationList()) { // for (com.a1.nextlocation.data.Location l : LocationListManager.INSTANCE.getLocationList()) {
GeoPoint p = new GeoPoint(l.getLat(), l.getLong()); // GeoPoint p = new GeoPoint(l.getLat(), l.getLong());
OverlayItem overlayItem = new OverlayItem(l.getName(),l.getDescription(), p); // OverlayItem overlayItem = new OverlayItem(l.getName(),l.getDescription(), p);
//
customOverlay.addOverlayItem(overlayItem); // customOverlay.addOverlayItem(overlayItem);
Log.d(TAG, "initMap: " + "succes"); // Log.d(TAG, "initMap: " + "succes");
} // }
mapView.getOverlays().add(customOverlay); // mapView.getOverlays().add(customOverlay);
// add the zoom controller // add the zoom controller
IMapController mapController = mapView.getController(); 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) { private void requestPermissionsIfNecessary(String... permissions) {
ArrayList<String> permissionsToRequest = new ArrayList<>(); ArrayList<String> permissionsToRequest = new ArrayList<>();

View File

@@ -38,7 +38,6 @@ public class RouteFragment extends Fragment {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(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) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
// ApiHandler.INSTANCE.getDirections(8.681436,49.41461,8.687872,49.420318); ApiHandler.INSTANCE.getDirections(51.49017262451581, 4.289038164073164,51.47337383133509, 4.303535222390562);
// Route r = new Route("test"); Route r = new Route("test");
// r.addLocation(new Location("test",8.681436,49.41461,"route",null)); r.addLocation(new Location("test",51.487963606716185, 4.280396603442448,"route",null));
// r.addLocation(new Location("test",8.687872,49.420318,"route",null)); r.addLocation(new Location("test",51.486962126272886, 4.27884897122147,"route",null));
// ApiHandler.INSTANCE.getDirections(r); 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));
public void onDirectionsAvailable(DirectionsResult result) { // r.addLocation(new Location("test",51.489063681658145, 4.289596063527951,"route",null));
Log.d(TAG, "onDirectionsAvailable: got result! " + result); // r.addLocation(new Location("test",51.483012677667766, 4.28003245468457,"route",null));
ApiHandler.INSTANCE.getDirections(r);
} }
} }

View File

@@ -15,6 +15,7 @@ import org.osmdroid.util.GeoPoint;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
public class DirectionsResult { public class DirectionsResult {
@@ -23,6 +24,7 @@ public class DirectionsResult {
private double distance; private double distance;
private double duration; private double duration;
private double[][] wayPointCoordinates; private double[][] wayPointCoordinates;
private GeoPoint[] startAndEndPoint = new GeoPoint[2];
public List<DirectionsStep> getSteps() { public List<DirectionsStep> getSteps() {
return steps; return steps;
@@ -52,8 +54,31 @@ public class DirectionsResult {
this.steps.add(step); 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<GeoPoint> 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<GeoPoint> 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 * 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. * @param json the json string to parse.
*/ */
public void parse(String json) { public void parse(String json) {
@@ -86,7 +111,7 @@ public class DirectionsResult {
for (JsonElement j : steps) { for (JsonElement j : steps) {
DirectionsStep step = gson.fromJson(j,DirectionsStep.class); DirectionsStep step = gson.fromJson(j, DirectionsStep.class);
double lat; double lat;
double longl; double longl;
@@ -94,7 +119,7 @@ public class DirectionsResult {
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
lat = this.wayPointCoordinates[step.getWay_points().get(i)][0]; lat = this.wayPointCoordinates[step.getWay_points().get(i)][0];
longl = this.wayPointCoordinates[step.getWay_points().get(i)][1]; 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); 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) { public void parseRoute(String json) {
@@ -115,7 +143,7 @@ public class DirectionsResult {
this.duration = summary.get("duration").getAsDouble(); this.duration = summary.get("duration").getAsDouble();
JsonPrimitive geometry = route.getAsJsonPrimitive("geometry"); 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]; this.wayPointCoordinates = new double[wayPointCoordinates.size()][2];
@@ -140,7 +168,7 @@ public class DirectionsResult {
for (JsonElement j : steps) { for (JsonElement j : steps) {
DirectionsStep step = gson.fromJson(j,DirectionsStep.class); DirectionsStep step = gson.fromJson(j, DirectionsStep.class);
double lat; double lat;
double longl; double longl;
@@ -148,7 +176,7 @@ public class DirectionsResult {
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
lat = this.wayPointCoordinates[step.getWay_points().get(i)][0]; lat = this.wayPointCoordinates[step.getWay_points().get(i)][0];
longl = this.wayPointCoordinates[step.getWay_points().get(i)][1]; 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); addStep(step);
@@ -158,7 +186,5 @@ public class DirectionsResult {
} }
} }
} }

View File

@@ -38,7 +38,7 @@ public enum ApiHandler {
} }
public void getDirections(double startLat, double startLong, double endLat, double endLong) { 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) { public void getDirections(String startLocation, String endLocation) {
@@ -69,6 +69,12 @@ public enum ApiHandler {
t.start(); t.start();
// try {
// t.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
} }
public void addListener(DirectionsListener listener) { public void addListener(DirectionsListener listener) {
@@ -104,6 +110,10 @@ public enum ApiHandler {
if (response.body() != null) { if (response.body() != null) {
String responseString = Objects.requireNonNull(response.body()).string(); String responseString = Objects.requireNonNull(response.body()).string();
Log.d(TAG, "getDirections: got response: " + responseString); Log.d(TAG, "getDirections: got response: " + responseString);
if (responseString.startsWith("{\"error")) {
Log.e(TAG, "getDirections: ERROR IN REQUEST!");
return;
}
DirectionsResult result = new DirectionsResult(); DirectionsResult result = new DirectionsResult();
result.parseRoute(responseString); result.parseRoute(responseString);
@@ -121,6 +131,12 @@ public enum ApiHandler {
t.start(); t.start();
// try {
// t.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
} }

View File

@@ -10,4 +10,5 @@
<color name="primaryColour">#FF115571</color> <color name="primaryColour">#FF115571</color>
<color name="secondaryColour">#FF31AFB4</color> <color name="secondaryColour">#FF31AFB4</color>
<color name="buttonColour">#FF14212D</color> <color name="buttonColour">#FF14212D</color>
<color name="red">#FF0000</color>
</resources> </resources>