Finding the closest Melbourne CBD train station with GDirections

Finding the closest Melbourne CBD train station with GDirections

This application> came from my dependence on public transport and possible need to move houses; I wanted to know how close I would be in relation to train stations. There are quite a number of stations in Melbourne, and I don’t have the time to map all of their coordinates, so at the time being it only has train stations from the City Loop.

The application uses GDirections. It lets us load way points and retrieve the route details; such as distance, driving directions, and even an HTML summary of the travel.

I used GDirections in two instances of the application:
1. To determine the distance between my pivot position and the train stations.
2. To plot the route of my pivot point to the closest train station

In both instances I used GDirections in the same way:

var directions = new GDirections();

// Put pivot location and the selected train station into an array   
var lArray = new Array();
lArray.push( start );
lArray.push( end );
   
// Load route from pivot location to train station
directions.loadFromWaypoints(lArray);

After I loaded the way points it is possible to retrieve information such as the distance in meters:

directions.getDistance().meters

However I ran into a problem: I was loading the way points but it was returning me NULL. That is because the loading is done asynchronously, so to retrieve the details we have to wait until it loads completely. To do that we create an event listener:

GEvent.addListener(directions,"load", function() 
{
   // Here directions should not be NULL
   var distance = directions.getDistance().meters;
});  

After we calculate the shortest distance, we are ready to render the route on the map. To do that I passed the map as an argument when initialising GDirections.

// Clear map and panel
map.clearOverlays();

var directions = new GDirections(map);
   
// Array with the shortest route
var lArray = new Array();
lArray.push( start );
lArray.push( end );
   
directions.loadFromWaypoints(lArray , {preserveViewport: true});

Note that I used the preserveViewport option. That is so the map won’t change zoom or position when rendering the route.

Please drop me a message if anyone knows where I can find free coordinates of the train stations, and I will add them to the application.

About these ads

2 Responses to “Finding the closest Melbourne CBD train station with GDirections”


  1. 1 tomas Saturday, 20 September 2008 at 2:52 am

    nice article if somebody intresting in some API coding u can check on my website:

    http://www.ontour.lt/distances-between-cities/
    http://www.ontour.lt/lithuania-world-map/

    i think u can find some scripts worth to look at too.

    Thanx again

  2. 2 banana health Friday, 26 April 2013 at 12:53 pm

    You can also follow the popular banana diet, which entails eating a raw banana and drinking room temperature water for breakfast.
    There are many diet plans that are available for you to
    try but make sure you have success with them. Luckily Asian
    women get thin and burn fat easily, even after giving birth, and
    do it in a healthy way that anyone can learn.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Hello

My name is Marcel Tjandraatmadja, and I live in Melbourne, Australia. I have a special interest in PHP and web applications.
I also enjoy problem solving with C#, C, and C++.

My interests are also Aikido, roller blading, and music from my favorite bands.

Curriculum Vitae

See my linked in page here.

Contact

Drop me a message at marcel.tjandra@gmail.com.

my del.icio.us

Posts

html hit counter

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: