使用谷歌API减少程序中的执行时间

时间:2016-03-01 06:43:18

标签: javascript php google-maps

此项目计算出发地和目的地之间的距离和行驶时间。为此,我使用谷歌API函数。该应用程序正常工作,但当程序的起源和目标将从数据库中获取时,它变得非常慢。有没有办法减少执行时间?

   <?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>

我需要定期更新有关该计划的信息,因此,需要定期进行计算 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

使用asp.net缓存或浏览器缓存(即localstorage)来缓存来自DB的结果。

ASP.NET Caching

Local Storage