added visualizing route
This commit is contained in:
18
app/src/main/java/com/a1/nextlocation/data/CurrentRoute.java
Normal file
18
app/src/main/java/com/a1/nextlocation/data/CurrentRoute.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
@@ -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<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
|
||||
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<String> permissionsToRequest = new ArrayList<>();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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<DirectionsStep> 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<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
|
||||
*
|
||||
* @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 {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -10,4 +10,5 @@
|
||||
<color name="primaryColour">#FF115571</color>
|
||||
<color name="secondaryColour">#FF31AFB4</color>
|
||||
<color name="buttonColour">#FF14212D</color>
|
||||
<color name="red">#FF0000</color>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user