# Calculating all distances from GPS route using date time and coordinates for php

I have big GPS data which have 4 columns for id, date time, lat, long. I want to implement Haversine's formula to get distance between 2 nodes.

If I want to find all distance from start to last using GPS data, then how do I set the parameters for \$lat1, \$lat2, \$lon1, \$lon2 in my code?

The final result is I can show all routes with the details of maximum and minimum's average speed on map. This is my code :

``````<?php
\$con=mysqli_connect('localhost','root','');
\$db=mysqli_select_db(\$con,'taxi');
\$rs=@mysqli_query(\$con,"SELECT * FROM taxi WHERE id=1 ORDER BY date ASC");
//the id is from ID each Taxi which have 100 ID and want to show each day routing
\$lat="";
\$i=1;
while (\$row = @mysqli_fetch_array(\$rs, MYSQLI_ASSOC)) {
if(\$i%2!=0){
//save the start node for latitude and longitude
\$awal_lt=\$row['lt'];
\$awal_lg=\$row['lg'];
}else{
echo getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2);
}
\$lat .= "{lat: ".\$row['lt'].", lng: ".\$row['lg']."},";
\$i++;
}
\$lat=substr(\$lat,0,(strlen(\$lat)-1));

//haversine formula
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}

return deg * (Math.PI/180)
}
?>

//this is my google map route
<!DOCTYPE html>
<html>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple Polylines</title>
<style>
html, body {
height: 100%;
margin: 0;
}
#map {
height: 100%;
}
</style>
<body>
<div id="map"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: {lat: 39.9185466, lng: 116.517268},
mapTypeId: 'terrain'
});
var flightPlanCoordinates = [<?php echo \$lat;?>
];
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
}
</script>
<script async defer