2

I have big GPS data which have 4 columns for id, date time, lat, long. I wanna implement Haversine's formula to get distance between 2 nodes. But if I want to find all distance from start to last using GPS data, how to 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 wanna 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 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>
<html>
  <head>
    <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;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <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;?>
        ];
        var flightPath = new google.maps.Polyline({
          path: flightPlanCoordinates,
          geodesic: true,
          strokeColor: '#FF0000',
          strokeOpacity: 1.0,
          strokeWeight: 2
        });
        flightPath.setMap(map);
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB_LrfJy2TmXyUT-ZNWgQdYeX5QuBmY7r4&callback=initMap">
    </script>
  </body>
</html>

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.