Report a driver's route segment to a server

In this example, a driver is using turn-by-turn directions and we want to report the driver's route to a server when the driver first starts on a route, and any time the driver reroutes or otherwise starts on a new route.

You will need to provide your own server for this. The data in our example is sent to the server with a PUT request in JSON format.

Before you do this:

In order to see results, first make sure you have turn-by-turn navigation up and running in your app. If you haven't done this yet, here's how .

After you present the turn-by-turn UI, you can register to be notified when the driver's route changes. (If you don't want to leave your desk to test this, it also works with our driving simulator , of course!)

final TGNavigationRepository.NavigationListener listener = new TGNavigationRepository.NavigationListener() {
    public void onRouteUpdated(TGRoute tgRoute) {
        if (tgRoute != null) {

    new TGNavigationRepository.AdapterCallback() {
        public void onReady(@NonNull TGNavigationRepository.Adapter adapter) {

//launch simulated navigation
TGLauncher.launchSimulatedNavigation(this, 2);

Once we've done that, let's write a method to report that data to our server.

private static final String REPORT_URL = "http://localhost:3200/drivers/route_segment";

private final UUID DRIVER_ID = UUID.randomUUID();

private TGRoute lastRoute;

private void reportRoute(final TGRoute route) {
    if (route == lastRoute) {
        //no assurances that you will never receive the same route through this interface
    lastRoute = route;

    //we use Volley but you may use whatever library you prefer
    RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest putRequest = new StringRequest(Request.Method.PUT, REPORT_URL,
        response -> {
        error -> {
            //failure to upload
    ) {
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            try {
                JSONObject json = route.getSegments().get(0).getJson();
                for (Iterator<String> it = json.keys(); it.hasNext(); ) {
                    String key =;
                    params.put(key, json.get(key).toString());
            } catch (JSONException ignored) {}
            return params;

        public Map<String, String> getHeaders() {
            final Map<String, String> headers = new HashMap<>();

            headers.put("X-Temporary-ID", DRIVER_ID.toString());
            return headers;
Check out the Android Reference App to see this example in action.