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?