Improve output of map tools
This commit is contained in:
parent
496c633ead
commit
1f4ae0e386
2 changed files with 51 additions and 4 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue