0

How to make the iteration query like FOR iteration to calculate distance between two nodes ($lat1, $lon1 to and $lat2, $lon2) and other coordinates etc on database using haversine formula to find the result from start to last node?

<?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 first node
$awal_lt=$row['lt'];
$awal_lg=$row['lg'];

}else{
//echo getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2)."<br>";
}
$lat .= "{lat: ".$row['lt'].", lng: ".$row['lg']."},";
$i++;
}
$lat=substr($lat,0,(strlen($lat)-1));

/*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)
}*/

?>

<!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 database : mysql

NabiLL
  • 1
  • 3
  • sorry did not see mysql... – Teng Ma Sep 22 '16 at 15:27
  • @AlexeySoshin yes, but there'is no iteration to calculate many coordinate, just two node. I hope I can find the iteration query to calculate two nodes etc continually with the haversine formula – NabiLL Sep 22 '16 at 16:07

0 Answers0