php使用谷歌地图API响应时间慢


php slow response time using google maps API

我有一个MySQL表,格式如下,大约有500行

ID¦name¦stuff¦location

我的php脚本获取用户位置,从数据库中选择所有行,然后使用googlemaps API获取从每个行位置到用户位置的距离,并将其全部放入json数组中

问题是,即使处理500行平均也需要300秒,我想以某种方式将其降低到大约3-4秒,似乎是谷歌地图API阻碍了整个脚本。

我想问的是,是否有更好的方法来处理整个事情,这样我就可以在10秒内让脚本处理所有事情。

php脚本-

<?php
//used for benchmarking
$time_start = microtime(true); 
require_once 'db.php';
//get the users location
$location = $_GET['location'];
                //check the users location is valid
           if (checkLocation($location) == "found") {
                  echo "null";
            } else {
                //select all of the rows
                $SQLSelect = $odb -> query("SELECT * FROM `something`");
                    $array = array();
                    $count = 0;
                    while ($show = $SQLSelect -> fetch(PDO::FETCH_ASSOC)) {
                        $distance = getDistance($location, $show['location']);
                            $distance = $distance * 0.000621371192;
                                        //narrow down the distance
                                        if ($distance < 100) {
$array[$count] = array();
$array[$count]['name'] = $show['locationName'];
$array[$count]['spec'] = $show['spec'];
$array[$count]['distance'] = round($distance);
                                        $count = $count + 1;
                                        }
                    }
                    //sort the array based on distance
usort($array,function($a,$b) {return strnatcasecmp($a['distance'],$b['distance']);});
                    echo json_encode($array);
            }

              function getDistance($start, $end) {
                    $from = $start;
                    $to = $end;
                    $from = urlencode($from);
                    $to = urlencode($to);
                    $data = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&language=en-EN&sensor=false");
                    $data = json_decode($data);
                    $time = 0;
                    $distance = 0;
                    foreach($data->rows[0]->elements as $road) {
                    $time += $road->duration->value;
                    $distance += $road->distance->value;
                    }
                    return $distance;
                    }

                    function checkLocation($start) {
                    $from = $start;
                    $to = 'location1';
                    $from = urlencode($from);
                    $to = urlencode($to);
                    $data = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&language=en-EN&sensor=false");
                    if (strpos($data, '"status" : "NOT_FOUND"') !== false) {
                    return "found";
                        } else {
                            return "null";
                        }
                }
                    echo '</br>Total execution time in seconds: ' . (microtime(true) - $time_start);        
?>

您唯一的加速选项是取消所有Google Maps API请求。我会更改您的脚本和数据库表,以使用纬度和经度点作为用户在数据库中的位置和项目。

可以使用MySQL计算两个lat和lng点之间的距离(就像乌鸦飞一样),如下所示:在MySQL 中使用纬度和经度查找两点之间的距离

如果你切换到这种方法,你肯定能够让你的脚本在<4秒。