Calculate all latitude and longitude distance in result using MySQL
Add something like this onto the query in order to get the TotalKM

GROUP BY id, gpslat, gpslon WITH ROLLUP


Don't attempt to get that exact output with SQL; do that in you application code.  The ROLLUP will give you an extra line something like

NULL NULL NULL 2.1