Merge branch 'master' of gitlab.com:jankstudio/resq

This commit is contained in:
Ramon Gutierrez 2018-01-17 18:12:54 +08:00
commit 1d4450821d
3 changed files with 91 additions and 54 deletions

View file

@ -50,3 +50,8 @@ services:
App\Service\FileUploader:
arguments:
$target_dir: '%image_upload_directory%'
App\Service\MapTools:
arguments:
$em: "@doctrine.orm.entity_manager"
$gmaps_api_key: "%env(GMAPS_API_KEY)%"

View file

@ -9,6 +9,7 @@ use CrEOF\Spatial\PHP\Types\Geometry\Point;
use App\Entity\Outlet;
use GuzzleHttp\Client as GuzzleClient;
use App\Service\MapTools;
class TestController extends BaseController
{
@ -38,64 +39,15 @@ class TestController extends BaseController
return $this->render('test/map.html.twig', $params);
}
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 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()
public function distance(MapTools $map_tools)
{
$params = $this->initParameters('home');
// $point = new Point(121.0495453, 14.6042567);
$point = new Point(120.343692, 16.048560);
$point = new Point(121.0495453, 14.6042567);
// $point = new Point(120.343692, 16.048560);
// test if we can get the top 5 closest outlets to a point
$em = $this->getDoctrine()->getManager();
$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);
$res = $map_tools->getClosestOutlets($point, 10);
error_log($res);
return $this->render('home.html.twig', $params);
}

80
src/Service/MapTools.php Normal file
View 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;
}
}