Jump to content

Recommended Posts

I am designing a Website of searching Buses stops and its timings i want to clear some concepts about how can i integrate Google Maps with my Database in PHP so that when i add some stops it will render from Google Maps and create a route pattern accordingly and saves to Database finally. for more clarity i have added a Pic :)  only just need to clear concept i don't want coding's i will do it myself. :)

post-193101-0-63533600-1476946029_thumb.png

It looks like you have a lot of duplication of data. Every time you add a bus, you add the stops also.

 

A better approach would be to define the route first.

Route definition                                   Stop 
+-----------+-----------+-----------+--------+     +----------+-----------------------+----------+-----------+
| route_id  | sequence  |  stop_id  |  mins  |     |  stop_id |  stop name            |   Lat    |   Long    |
+-----------+-----------+-----------+--------+     +----------+-----------------------+----------+-----------+
|   DA      |     1     |    102    |     0  |     |    101   |  Agra                 | 27.16945 | 78.01194  |
|   DA      |     2     |    103    |    10  |     |    102   |  Dehli                | 28.60357 | 77.26217  |
|   DA      |     3     |    104    |   190  |     |    103   |  Noida                |         etc          |
|   DA      |     4     |    101    |   250  |     |    104   |  Vrindavan            |         etc          |
+-----------+-----------+-----------+--------+     +----------+-----------------------+----------+-----------+

Now you can add the timetabled journeys to run on each route

Timetabled journey
+----------+-------------+----------+
| jny_id   |  route_id   |  depart  |
+----------+-------------+----------+
|    450   |     DA      |   7:50   |
|    451   |     DA      |   9:50   |

Intermediate stop times are then calculated from the route definition.

 

You can take it further if you have a table to allocate the "bus working" defining the sets of journeys scheduled for a bus. This enables you to produce service timetable as well as vehicle timetables.

 

To show on a map first create a map object.

For each stop,

  • create a LatLng object using the stop's latitude and longitude values
  • add marker object to map object using the LatLng object

[EDIT] Note that the above would be done in Javascript. You would use PHP to retrieve the data required and pass it to the client via an AJAX request.

Edited by Barand

It looks like you have a lot of duplication of data. Every time you add a bus, you add the stops also.

 

A better approach would be to define the route first.

Route definition                                   Stop 
+-----------+-----------+-----------+--------+     +----------+-----------------------+----------+-----------+
| route_id  | sequence  |  stop_id  |  mins  |     |  stop_id |  stop name            |   Lat    |   Long    |
+-----------+-----------+-----------+--------+     +----------+-----------------------+----------+-----------+
|   DA      |     1     |    102    |     0  |     |    101   |  Agra                 | 27.16945 | 78.01194  |
|   DA      |     2     |    103    |    10  |     |    102   |  Dehli                | 28.60357 | 77.26217  |
|   DA      |     3     |    104    |   190  |     |    103   |  Noida                |         etc          |
|   DA      |     4     |    101    |   250  |     |    104   |  Vrindavan            |         etc          |
+-----------+-----------+-----------+--------+     +----------+-----------------------+----------+-----------+

Now you can add the timetabled journeys to run on each route

Timetabled journey
+----------+-------------+----------+
| jny_id   |  route_id   |  depart  |
+----------+-------------+----------+
|    450   |     DA      |   7:50   |
|    451   |     DA      |   9:50   |

Intermediate stop times are then calculated from the route definition.

 

You can take it further if you have a table to allocate the "bus working" defining the sets of journeys scheduled for a bus. This enables you to produce service timetable as well as vehicle timetables.

 

To show on a map first create a map object.

For each stop,

  • create a LatLng object using the stop's latitude and longitude values
  • add marker object to map object using the LatLng object

[EDIT] Note that the above would be done in Javascript. You would use PHP to retrieve the data required and pass it to the client via an AJAX request.

sir you suggested to show route on Map but i want to retrieve route information from Map so that all things like speed and distance of intermediate will come automatically only i have to feed stops and press ADD, if i do much calculation then all my time will be going to be wasted on adding Database,

I haven't used the Google Distance Matrix API before so this was a learning exercise for me. Here's a small application:

 

  • route_1.pgp - main page with map display
  • route_2.php - responds to AJAX calls to retrieve data
  • route.sql.txt - SQL script file to create database (Note mins and distances are set to zero in the route_definition table)

When you load route_1, the routes are listed. Clicking on a route fetches the route definition from the database, submits a request to the google api to get the times and distances and updates the route records in the table. The distances and times are then listed on the screen and the stops are displayed on the map.

 

I hope this helps to get you going.

routes.sql.txt

routes_1.php

routes_2.php

post-3105-0-65530100-1477075918_thumb.png

Edited by Barand

I haven't used the Google Distance Matrix API before so this was a learning exercise for me. Here's a small application:

 

  • route_1.pgp - main page with map display
  • route_2.php - responds to AJAX calls to retrieve data
  • route.sql.txt - SQL script file to create database (Note mins and distances are set to zero in the route_definition table)

When you load route_1, the routes are listed. Clicking on a route fetches the route definition from the database, submits a request to the google api to get the times and distances and updates the route records in the table. The distances and times are then listed on the screen and the stops are displayed on the map.

 

I hope this helps to get you going.

Great sir ! i am pleased that you get a topic to learn because of me :)

btw i am getting a error while executing the Routes_1.php file " Fatal error: Call to a member function fetch_row() on boolean in C:\xampp\htdocs\yatrika\route1.php on line 13 "

From where you are calling a Fetch Row Function ?

That would be because the query failed

        // get the google maps key
        $sql = "SELECT gm_key 
                  , ROUND(home_lat,2)
                  , ROUND(home_long,2)
                FROM mydb.google_map_key";
        $res = $db->query($sql);
        list($gmkey, $home_lat, $home_lng) = $res->fetch_row();


As the comment says, that query fetches my personal Google API key from a database (plus the starting map coordinates for the application. I suggest you provide those three values as hard-coded values for now to get it working.
 
Remove the above code and replace with

$home_lat = 27.60;
$home_lng = 77.06;
$gmkey = "<your api key goes here>";

 
I did some more testing today. "route_2" works only if the places on the route are on a straight route IE for
 

A - B - C - D

 
If you go off the line as in
 
 

A - B   D
    |   |
     \  |
       C

then it give the time for AD as less than the time AC. So the code needs changing so, instead of calculating AB, AC, AD as it does now, it calculates AB, BC, CD and accumulates the times.

 

Revised route2 attached.

routes_2.php

Does it work if you link to a local jquery.js file instead of the google one?

Nothing happen. is code working on your Localhost i am using  XAMPP 5.6.3 [php: 7.0.3] XAMPP CP V3.2.2 Compiled Nov 12th 2015

also tested on XAMPP Version 5.6.20

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.