Report a driver's current location to a server

In this example, we are continuously monitoring the driver's location as they follow turn-by-turn directions. We report the location to the server at regular intervals. Specifically, any time the driver moves more than 100 meters from the last report, or 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 location 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 onRouteLocationUpdated(Location location) {
        reportLocation(location);
    }
};

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 double DISTANCE_BETWEEN_REPORTS = 100d; //meters
private static final long DURATION_BETWEEN_REPORTS = 60000L; //ms
private static final String REPORT_URL = "http://localhost:3200/drivers/current_location";

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

private void reportLocation(Location location) {
    final LatLng coordinate = TGUtils.locToCoor(location);
    if (coordinate == null) {
        return;
    }

    if (lastReportedPoint != null
            && TGMapUtils.metersBetweenPoints(lastReportedPoint, coordinate) < DISTANCE_BETWEEN_REPORTS
            && System.currentTimeMillis() < lastReportedTime + DURATION_BETWEEN_REPORTS) {
        //do not report unless one is satisfied
        return;
    }
    lastReportedTime = System.currentTimeMillis();
    lastReportedPoint = coordinate;

    //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("latitude", String.valueOf(coordinate.getLatitude()));
            params.put("longitude", String.valueOf(coordinate.getLongitude()));
            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.