From 1f4ae0e3866a01e198f8d73665769e9041e2b9ff Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Wed, 17 Jan 2018 22:51:13 +0800 Subject: [PATCH] Improve output of map tools --- src/Controller/TestController.php | 8 +++++- src/Service/MapTools.php | 47 +++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/Controller/TestController.php b/src/Controller/TestController.php index e94fc969..f21dc537 100644 --- a/src/Controller/TestController.php +++ b/src/Controller/TestController.php @@ -10,6 +10,7 @@ use CrEOF\Spatial\PHP\Types\Geometry\Point; use App\Entity\Outlet; use GuzzleHttp\Client as GuzzleClient; use App\Service\MapTools; +use Doctrine\Common\Util\Debug; class TestController extends BaseController { @@ -47,7 +48,12 @@ class TestController extends BaseController // $point = new Point(120.343692, 16.048560); $res = $map_tools->getClosestOutlets($point, 10); - error_log($res); + foreach ($res as $data) + { + error_log($data['db_distance']); + error_log($data['distance']); + error_log($data['duration']); + } return $this->render('home.html.twig', $params); } diff --git a/src/Service/MapTools.php b/src/Service/MapTools.php index ed8d14b6..7dd502fb 100644 --- a/src/Service/MapTools.php +++ b/src/Service/MapTools.php @@ -66,15 +66,56 @@ class MapTools $result = $query->getResult(); $outlets = []; + $final_data = []; foreach ($result as $row) { - error_log($row[0]->getName() . ' - ' . $row['dist']); + // error_log($row[0]->getName() . ' - ' . $row['dist']); $outlets[] = $row[0]; + $final_data[] = [ + 'outlet' => $row[0], + 'db_distance' => $row['dist'], + 'distance' => 0, + 'duration' => 0, + ]; } // get actual distance details with eta from google maps api - $res = $this->mapGetDistances($point, $outlets); + $raw_res = $this->mapGetDistances($point, $outlets); + $res = json_decode($raw_res, true); + // error_log(print_r($res, true)); - return $res; + // check if status is ok + if ($res['status'] != 'OK') + { + return $final_data; + // error_log('status not ok'); + } + + // check that the elements array is there + if (!isset($res['rows'][0]['elements'])) + { + // error_log('no elements'); + return $final_data; + } + + foreach($res['rows'][0]['elements'] as $index => $gm_row) + { + // check status + if (isset($gm_row['status']) && $gm_row['status'] != 'OK') + { + // error_log('element row status not ok'); + continue; + } + + // set distance + if (isset($gm_row['distance']['value'])) + $final_data[$index]['distance'] = $gm_row['distance']['value']; + + // set duration + if (isset($gm_row['duration']['value'])) + $final_data[$index]['duration'] = $gm_row['duration']['value']; + } + + return $final_data; } }