Merge branch 'master' of gitlab.com:jankstudio/resq
This commit is contained in:
commit
1d4450821d
3 changed files with 91 additions and 54 deletions
|
|
@ -50,3 +50,8 @@ services:
|
||||||
App\Service\FileUploader:
|
App\Service\FileUploader:
|
||||||
arguments:
|
arguments:
|
||||||
$target_dir: '%image_upload_directory%'
|
$target_dir: '%image_upload_directory%'
|
||||||
|
|
||||||
|
App\Service\MapTools:
|
||||||
|
arguments:
|
||||||
|
$em: "@doctrine.orm.entity_manager"
|
||||||
|
$gmaps_api_key: "%env(GMAPS_API_KEY)%"
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
||||||
|
|
||||||
use App\Entity\Outlet;
|
use App\Entity\Outlet;
|
||||||
use GuzzleHttp\Client as GuzzleClient;
|
use GuzzleHttp\Client as GuzzleClient;
|
||||||
|
use App\Service\MapTools;
|
||||||
|
|
||||||
class TestController extends BaseController
|
class TestController extends BaseController
|
||||||
{
|
{
|
||||||
|
|
@ -38,64 +39,15 @@ class TestController extends BaseController
|
||||||
return $this->render('test/map.html.twig', $params);
|
return $this->render('test/map.html.twig', $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function mapGetDistances(Point $point, $outlets)
|
public function distance(MapTools $map_tools)
|
||||||
{
|
|
||||||
$client = new GuzzleClient();
|
|
||||||
|
|
||||||
// origins
|
|
||||||
$origins_value = $point->getLatitude() . ',' . $point->getLongitude();
|
|
||||||
|
|
||||||
// destinations
|
|
||||||
$dests = [];
|
|
||||||
foreach ($outlets as $outlet)
|
|
||||||
{
|
|
||||||
$coord = $outlet->getCoordinates();
|
|
||||||
$dests[] = $coord->getLatitude() . ',' . $point->getLongitude();
|
|
||||||
}
|
|
||||||
$dests_value = implode('|', $dests);
|
|
||||||
|
|
||||||
// google maps base url api
|
|
||||||
$maps_url = 'https://maps.googleapis.com/maps/api/distancematrix/json';
|
|
||||||
|
|
||||||
// parameters
|
|
||||||
$gmaps_params = [
|
|
||||||
'origins' => $origins_value,
|
|
||||||
'destinations' => $dests_value,
|
|
||||||
];
|
|
||||||
|
|
||||||
error_log(print_r($gmaps_params, true));
|
|
||||||
|
|
||||||
// query google maps api
|
|
||||||
$res = $client->request('GET', $maps_url, ['query' => $gmaps_params]);
|
|
||||||
error_log($res->getBody());
|
|
||||||
|
|
||||||
return $res;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function distance()
|
|
||||||
{
|
{
|
||||||
$params = $this->initParameters('home');
|
$params = $this->initParameters('home');
|
||||||
|
|
||||||
// $point = new Point(121.0495453, 14.6042567);
|
$point = new Point(121.0495453, 14.6042567);
|
||||||
$point = new Point(120.343692, 16.048560);
|
// $point = new Point(120.343692, 16.048560);
|
||||||
|
|
||||||
// test if we can get the top 5 closest outlets to a point
|
$res = $map_tools->getClosestOutlets($point, 10);
|
||||||
$em = $this->getDoctrine()->getManager();
|
error_log($res);
|
||||||
$query = $em->createQuery('SELECT o, st_distance(o.coordinates, point(:lng, :lat)) as dist FROM App\Entity\Outlet o ORDER BY dist')
|
|
||||||
->setParameter('lat', $point->getLatitude())
|
|
||||||
->setParameter('lng', $point->getLongitude())
|
|
||||||
->setMaxResults(5);
|
|
||||||
error_log($query->getSql());
|
|
||||||
$result = $query->getResult();
|
|
||||||
|
|
||||||
$outlets = [];
|
|
||||||
foreach ($result as $row)
|
|
||||||
{
|
|
||||||
error_log($row[0]->getName() . ' - ' . $row['dist']);
|
|
||||||
$outlets[] = $row[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->mapGetDistances($point, $outlets);
|
|
||||||
|
|
||||||
return $this->render('home.html.twig', $params);
|
return $this->render('home.html.twig', $params);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
80
src/Service/MapTools.php
Normal file
80
src/Service/MapTools.php
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Service;
|
||||||
|
|
||||||
|
use GuzzleHttp\Client as GuzzleClient;
|
||||||
|
use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
||||||
|
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
|
class MapTools
|
||||||
|
{
|
||||||
|
const URL_DISTANCE_MATRIX = 'https://maps.googleapis.com/maps/api/distancematrix/json';
|
||||||
|
|
||||||
|
// entity manager
|
||||||
|
protected $em;
|
||||||
|
|
||||||
|
// google maps api key
|
||||||
|
protected $gmaps_api_key;
|
||||||
|
|
||||||
|
public function __construct(EntityManagerInterface $em, $gmaps_api_key)
|
||||||
|
{
|
||||||
|
$this->em = $em;
|
||||||
|
$this->gmaps_api_key = $gmaps_api_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function mapGetDistances(Point $point, $outlets)
|
||||||
|
{
|
||||||
|
$client = new GuzzleClient();
|
||||||
|
|
||||||
|
// origins
|
||||||
|
$origins_value = $point->getLatitude() . ',' . $point->getLongitude();
|
||||||
|
|
||||||
|
// destinations
|
||||||
|
$dests = [];
|
||||||
|
foreach ($outlets as $outlet)
|
||||||
|
{
|
||||||
|
$coord = $outlet->getCoordinates();
|
||||||
|
$dests[] = $coord->getLatitude() . ',' . $point->getLongitude();
|
||||||
|
}
|
||||||
|
$dests_value = implode('|', $dests);
|
||||||
|
|
||||||
|
// google maps url
|
||||||
|
$maps_url = self::URL_DISTANCE_MATRIX;
|
||||||
|
|
||||||
|
// parameters
|
||||||
|
$gmaps_params = [
|
||||||
|
'origins' => $origins_value,
|
||||||
|
'destinations' => $dests_value,
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
// query google maps api
|
||||||
|
$res = $client->request('GET', $maps_url, ['query' => $gmaps_params]);
|
||||||
|
|
||||||
|
return $res->getBody();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getClosestOutlets(Point $point, $limit)
|
||||||
|
{
|
||||||
|
// get closest outlets based on st_distance function from db
|
||||||
|
$query = $this->em->createQuery('SELECT o, st_distance(o.coordinates, point(:lng, :lat)) as dist FROM App\Entity\Outlet o ORDER BY dist')
|
||||||
|
->setParameter('lat', $point->getLatitude())
|
||||||
|
->setParameter('lng', $point->getLongitude())
|
||||||
|
->setMaxResults($limit);
|
||||||
|
// error_log($query->getSql());
|
||||||
|
$result = $query->getResult();
|
||||||
|
|
||||||
|
$outlets = [];
|
||||||
|
foreach ($result as $row)
|
||||||
|
{
|
||||||
|
error_log($row[0]->getName() . ' - ' . $row['dist']);
|
||||||
|
$outlets[] = $row[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
// get actual distance details with eta from google maps api
|
||||||
|
$res = $this->mapGetDistances($point, $outlets);
|
||||||
|
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue