Skip to content

Commit

Permalink
Adjust coordinate values when requesting in RouteEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder committed Jan 18, 2018
1 parent 9c5c5e8 commit e6fdfb4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
import com.google.gson.GsonBuilder;
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.geojson.BoundingBox;
import com.mapbox.geojson.Geometry;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.gson.BoundingBoxDeserializer;
import com.mapbox.geojson.gson.GeoJsonAdapterFactory;
import com.mapbox.geojson.gson.GeometryDeserializer;
import com.mapbox.geojson.gson.PointDeserializer;
import com.mapbox.services.android.navigation.v5.navigation.NavigationConstants;

import java.util.HashMap;
Expand Down Expand Up @@ -68,9 +74,18 @@ public static void startNavigation(Activity activity, NavigationViewOptions opti
*/
static DirectionsRoute extractRoute(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String directionsRoute = preferences.getString(NavigationConstants.NAVIGATION_VIEW_ROUTE_KEY, "");
return new GsonBuilder().registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create()
.fromJson(directionsRoute, DirectionsRoute.class);
String directionsRouteJson = preferences.getString(NavigationConstants.NAVIGATION_VIEW_ROUTE_KEY, "");
return directionsRouteFromJson(directionsRouteJson);
}

private static DirectionsRoute directionsRouteFromJson(String json) {
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapter(Point.class, new PointDeserializer());
gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer());
gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer());
gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
return gson.create().fromJson(json, DirectionsRoute.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public boolean shouldCheckFasterRoute(Location location, RouteProgress routeProg
lastCheckedLocation = location;
}
// Check if the faster route time interval has been exceeded
if (secondsSinceLastCheck(location) > NAVIGATION_CHECK_FASTER_ROUTE_INTERVAL) {
if (secondsSinceLastCheck(location) >= NAVIGATION_CHECK_FASTER_ROUTE_INTERVAL) {
lastCheckedLocation = location;
// Check for both valid route and step durations remaining
if (validRouteDurationRemaining(routeProgress) && validStepDurationRemaining(routeProgress)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mapbox.services.android.navigation.v5.navigation.NavigationRoute;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
Expand All @@ -30,23 +31,28 @@ public RouteEngine(Callback engineCallback) {
public void fetchRoute(Point origin, RouteProgress routeProgress) {
if (routeProgress == null) {
return;
} else {
this.routeProgress = routeProgress;
}
this.routeProgress = routeProgress;

// Calculate remaining waypoints
List<Point> coordinates = routeProgress.directionsRoute().routeOptions().coordinates();
List<Point> remainingCoordinates = coordinates.subList(0, routeProgress.remainingWaypoints());
Point destination = remainingCoordinates.remove(remainingCoordinates.size() - 1);
List<Point> coordinates = new ArrayList<>(routeProgress.directionsRoute().routeOptions().coordinates());

// Build new route request with current route options
if (coordinates.size() < routeProgress.remainingWaypoints()) {
return;
}
// Remove any waypoints that have been passed
coordinates.subList(0, routeProgress.remainingWaypoints()).clear();
// Get the destination waypoint (last in the list)
Point destination = coordinates.remove(coordinates.size() - 1);

// Build new route request with the given origin and current route options
RouteOptions currentOptions = routeProgress.directionsRoute().routeOptions();
NavigationRoute.Builder builder = NavigationRoute.builder()
.origin(origin)
.routeOptions(currentOptions);

// Add waypoints with the remaining coordinate values
addWaypoints(remainingCoordinates, builder);
addWaypoints(coordinates, builder);

builder.destination(destination);
builder.build().getRoute(this);
Expand Down

0 comments on commit e6fdfb4

Please sign in to comment.