Add Waypoint During Navigation

Not only can you specify a list of waypoints upfront , you can also dynamically add waypoints while turn-by-turn navigation is in progress. This could be ideal for use cases where you have a central dispatch service and want to reroute drivers to new locations automatically, without requiring the driver to take any action.

  1. First off, create and setup a route request with your desired waypoints:
    final TGRouteRequest routeRequest = new TGRouteRequest.Builder()
                         .addCoordinate(34.119190d, -118.300348d) //Griffith Observatory (origin)
                         .addCoordinate(34.011441d, -118.494932d) //Santa Monica Pier (destination)
                         .build();
  2. If you're testing at your desk, you'll probably want to simulate your location, as before. However, skip this block if you want to test in the real world.
    LatLng simulatedCurrentLocation = routeRequest.getWayPoints().get(0).getCoordinate(); // Start from the first waypoint
    boolean simulate = true;
    boolean clickToUpdate = false;
    
    TallyGo.getInstance().enableSimulation(simulate, clickToUpdate, simulatedCurrentLocation);
  3. Next, launching the turn-by-turn navigation:
    TGLauncher.startTurnByTurn(CONTEXT, routeRequest);
  4. In this next part, we will simulate what it would be like if the central dispatch were to send the driver a new waypoint and want to automatically reroute the driver immediately. For our example purposes, instead of a server, we will use a simple 30-second timer after navigation begins.
    new Handler(Looper.getMainLooper()).postDelayed(() -> {
      TGWaypoint newWaypoint = new TGWaypoint(34.101558d, -118.340944d); // New Waypoint: Grauman's Chinese Theatre
    
      addNewWaypoint(newWaypoint);
    }, 30 * 1000);
    
    private void addNewWaypoint(TGWaypoint newWaypoint) {
      TGNavigationRepository.addWaypointToStartOfActiveRoute(new TGNavigationRepository.RerouteResultListener() {
          @Override
          public void onSuccess(List<TGWaypoint> waypointsInNewRoute) {
              TGToastHelper.showLong(CONTEXT, "A new delivery order has come in. You are being rerouted to a new destination...");
          }
      }, newWaypoint);
    }
Check out the Android Reference App to see this example in action.