Report a driver's ETA to a server

In this example, we are continuously monitoring the driver's ETA as they follow turn-by-turn directions. We report the ETA to the server at regular intervals. Specifically, when 60 seconds has elapsed since the last report.

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 ETA 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() {
    @Override
    public void onArrivalInfoUpdated(@Nullable TGArrivalInfo tgArrivalInfo) {
        if (tgArrivalInfo != null) {
            reportEta(tgArrivalInfo.getArrivalDate());
        }
    }
};

TGNavigationRepository.getDefaultNavigationAdapter(getApplication(), 
    new TGNavigationRepository.AdapterCallback() {
        @Override
        public void onReady(@NonNull TGNavigationRepository.Adapter adapter) {
            adapter.setNavigationListener(listener);
        }
});

//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 long DURATION_BETWEEN_REPORTS = 60000L; //ms
private static final String REPORT_URL = "http://localhost:3200/drivers/eta";

private final UUID DRIVER_ID = UUID.randomUUID();
private long lastReportedTime;

private void reportEta(final Date arrivalDate) {
    if (System.currentTimeMillis() < lastReportedTime + DURATION_BETWEEN_REPORTS) {
        //do not report unless 60 sec have passed since last report
        return;
    }
    lastReportedTime = System.currentTimeMillis();

    //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 -> {
            //success
        },
        error -> {
            //failure to upload
        }
    ) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String>  params = new HashMap<>();
            params.put("ETA", TGUtils.getUtcTime(arrivalDate));
            return params;
        }

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

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