-1

This project calculating the distance and driving time between the origin and destination. for that I used google API function. The app works properly but when the origin and destination of the program will fetch from database , it becomes very slow. Is there any way to reduce execution time?

   <?php
define('INCLUDE_CHECK',1);
include "classes/dbconnect.php";

?>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
<?php
  $queryStaton = "select * from stations";
    $queryBus = "select * from locations";
    $result=select_all($queryStaton);
    $resultBus=select_all($queryBus);
    if ($num=mysql_numrows($result)) {
      $i=0;
      for($i;$i<$num;$i++) {
        $id=mysql_result($result,$i,"id");
        $stationName=mysql_result($result,$i,"stationName");
        $stationLant=mysql_result($result,$i,"lant");
        $stationLong=mysql_result($result,$i,"long");
        $stationDistance[$id] = $stationName.','.$stationLant.','.$stationLong;
        }
     }
      else{echo "No Connection!!!";}
       if ($number=mysql_numrows($resultBus)) {
          $j=0;
          for($j;$j<$number;$j++) {
            $busid=mysql_result($resultBus,$j,"id");
            $busLant=mysql_result($resultBus,$j,"lapt");
            $busLong=mysql_result($resultBus,$j,"long");
            $busTitle=mysql_result($resultBus,$j,"title");
            $busDistance[$busid] = $busTitle.','.$busLant.','.$busLong;
          }
        }

       for($count=0;$count<=count($stationDistance);$count++){
          $station_info = explode(',',$stationDistance[$count]);
          for($c=0;$c<=count($busDistance);$c++){
            $bus_info = explode(',',$busDistance[$c]);

?>
var origin[] = new google.maps.LatLng(<?php echo $station_info[1]?>, <?php echo $station_info[2]?>),
    destination[] =new google.maps.LatLng(<?php echo $bus_info[1]?>,<?php echo $bus_info[1]?>),
    service = new google.maps.DistanceMatrixService();
  <?php
  }
  }

?>
service.getDistanceMatrix(
    {
        origins: [origin],
        destinations: [destination],
        travelMode: google.maps.TravelMode.DRIVING,
        avoidHighways: false,
        avoidTolls: false
    },
    callback
);

function callback(response, status) {
    var orig = document.getElementById("orig"),
        dest = document.getElementById("dest"),
        dist = document.getElementById("dist"),
        time= document.getElementById("time");

    if(status=="OK") {
        orig.value = response.destinationAddresses[0];
        dest.value = response.originAddresses[0];
        dist.value = response.rows[0].elements[0].distance.text;
        time.value = response.rows[0].elements[0].duration.text;
    } else {
        alert("Error: " + status);
    }
}
</script>
</head>
<body>
    <br>
    Basic example for using the Distance Matrix.<br><br>
    Origin: <input id="orig" type="text" style="width:35em"><br><br>
    Destination: <input id="dest" type="text" style="width:35em"><br><br>
    Distance: <input id="dist" type="text" style="width:35em"><br><br>
    Time: <input id="time" type="text" style="width:35em">
</body>
</html>

I need to be regularly updated information on the program, therefore, is required calculations to be performed regularly Can someone help please?

m.moghadam
  • 82
  • 8
  • 1
    Can you post your database part? –  Mar 01 '16 at 06:47
  • Unfortunately i don't have that part now, but i can tell you what information i receiving information from data base. Longitude and latitude of origin and Destination are store in a table in my database – m.moghadam Mar 01 '16 at 06:54
  • you get it with Ajax , or normal way ? –  Mar 01 '16 at 06:55
  • look , several thing is important for speed. 1- your query 2-your server 3-your optimize code 4-your data base schema etc . i must see what are you doing in all of your project then , maybe i can figure out how to improve speed –  Mar 01 '16 at 07:02
  • right now i'm working with local host server. even without using query and database it work very slowly – m.moghadam Mar 01 '16 at 07:06
  • Madam, if you talk about performance, you show us server code, not JS. – alsafoo Mar 01 '16 at 07:08
  • change your browser. did you try for getting `src="http://maps.google.com/maps/api/js?sensor=false"` and put it in your local system ? i think it cause your problem –  Mar 01 '16 at 07:13
  • Would you say that if i put my program into server that my problem will solve? – m.moghadam Mar 01 '16 at 07:15
  • not sure , but , you can see this affect your codes speed. –  Mar 01 '16 at 07:19

1 Answers1

0

Use asp.net caching or browser caching (i.e. localstorage) to cache result from DB.

ASP.NET Caching

Local Storage

Community
  • 1
  • 1
alsafoo
  • 778
  • 4
  • 18
  • @alsafoo , You know that , Better developer works on PHP ! ;) , not GUI or drag and drop things –  Mar 01 '16 at 07:21
  • You don't know what you missing! Get a life bro! A .NET life. And btw, neither asp.net caching nor local storage are drag and drop. – alsafoo Mar 01 '16 at 07:28