Skip to content

Commit

Permalink
Adds google route to bustracker routes
Browse files Browse the repository at this point in the history
  • Loading branch information
LaercioSantana committed Jul 19, 2016
1 parent ca4df9e commit 5cfe94a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ application/config/database.php
application/config/config.php
docs
user_guide
application/google_routes/*
2 changes: 2 additions & 0 deletions application/config/config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,5 @@
*/

$BusTrackerConfig["CRYPT_KEY"] = "mysecretcryptkey";
$BusTrackerConfig["SALT_USER_PASSWORD"] = "mysalt";
$BusTrackerConfig["GOOGLE_SERVER_KEY"] = "AIza...w4";
44 changes: 44 additions & 0 deletions application/controllers/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function getRoutes(){
if($this->input->get("points") === "true"){
foreach ($routes as $route){
$route->points = $this->routes_model->getPoints($route->id_routes);
$route->googleRoute = $this->getGoogleRoute($route->points);
}
}

Expand Down Expand Up @@ -142,6 +143,12 @@ function addRoute(){
if($validator->valid){
$this->loadModel();
$input = json_decode($this->input->raw_input_stream);
$input->googleRoute = $this->getGoogleRoute($input->points);
$googleRoute = json_decode($this->getGoogleRoute($input->points), JSON_NUMERIC_CHECK);
// check errors in google route
if(isset($googleRoute->error_message))
return $this->makeJsonRespose($input->googleRoute, 400);

$id = $this->routes_model->insertRoute($input);

return $this->makeJsonRespose(["id" => $id], 201);
Expand Down Expand Up @@ -326,4 +333,41 @@ function deleteRoute($id){

return $this->makeJsonRespose(["id" => $id], $statusCode);
}
private function getGoogleRoute($points){
if(count($points) < 2)
return json_encode(['error_message' => 'At least 2 points is required',
'statusCode' => 400], JSON_NUMERIC_CHECK);

//make way points
$waypoints = "";
for($i = 1; $i < count($points) - 1;$i++)
$waypoints = $waypoints.$points[$i]->latitude.",".$points[$i]->longitude."|";

$origin = $points[0];
$destination = $points[count($points)-1];
$request = "https://maps.googleapis.com/maps/api/directions/json?".
"origin={$origin->latitude},{$origin->longitude}&".
"destination={$destination->latitude},{$destination->longitude}&".
"waypoints=via:{$waypoints}&".
"key=".Routes::GOOGLE_SERVER_KEY();

return Routes::requestGet($request);
}
private static function requestGet($url){
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url
));

$resp = curl_exec($curl);

curl_close($curl);
return $resp;
}
private static function GOOGLE_SERVER_KEY(){
global $BusTrackerConfig;
return $BusTrackerConfig["GOOGLE_SERVER_KEY"];
}
}
11 changes: 10 additions & 1 deletion application/models/Routes_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ class Routes_model extends CI_Model{
const table = 'routes';
const pointsTable = 'points_route';
const busTable = 'bus';
public static $google_routes_path;

public function __construct(){
parent::__construct();
self::$google_routes_path = APPPATH."google_routes/";
}
public function index(){
return $this->db->get(self::table)->result();
Expand All @@ -20,8 +22,12 @@ public function getRoute($id){
->where(self::table.".id_routes = {$id}")
->get()->result();

if(isset($route[0]))
if(isset($route[0])){
//recover google route
$googleRoute = file_get_contents(self::$google_routes_path.$id);
$route[0]->googleRoute = json_decode($googleRoute, JSON_NUMERIC_CHECK);
return $route[0];
}

return null;
}
Expand All @@ -48,6 +54,9 @@ public function insertRoute($route){

$this->db->insert_batch(self::pointsTable, $points);

//write google route information
file_put_contents(self::$google_routes_path.$id, $route->googleRoute);

return $id;
}
public function getBuses($id){
Expand Down

0 comments on commit 5cfe94a

Please sign in to comment.