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 :

$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
while ($row = @mysqli_fetch_array($rs, MYSQLI_ASSOC)) {
//save the start node for latitude and longitude
 echo getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2);
$lat .= "{lat: ".$row['lt'].", lng: ".$row['lg']."},";

 //haversine formula
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1);  // deg2rad below
var dLon = deg2rad(lon2-lon1); 
var a = 
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
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;

function deg2rad(deg) {
return deg * (Math.PI/180)

//this is my google map route
<!DOCTYPE html>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Simple Polylines</title>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      #map {
        height: 100%;
    <div id="map"></div>
      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;?>
        var flightPath = new google.maps.Polyline({
          path: flightPlanCoordinates,
          geodesic: true,
          strokeColor: '#FF0000',
          strokeOpacity: 1.0,
          strokeWeight: 2
    <script async defer

This is the result without average speed :image

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.