iPhone Brighton Buses webapp/javascript front-end


Here in Brighton a good number of the bus stops have electronic boards, that tell you when the next buses are due. The data for these boards is available online and even provides data for stops that don't have the electronic boards. In fact there's an free iPhone app available, so you can get the data easily on the move.


Brighton Station (Stop B)

After playing around with iPhone javascript apps, I thought it would be interesting to try and create a javascript app for the bus times. I had some specific ideas about how I wanted the app to work. I was also keen on trying out some more "HTML 5" tech. Plus I do like things to be (at least in principle) cross-platform, so this has a chance of working ok on Android phones (though I've not tested it on one).

There are still a few rough edges, but you can try out the app yourself at:

It will prompt you to add a stop. If you type in the text field, you'll get a list of stops that match. If your browser supports geo-location, you'll also see a "Find nearby" button, which will list the nearest few stops.

I had the pleasure of demoing this app at the Async Javascript show'n'tell last month. I quickly outlined how it worked, which involves the following:

Getting the data

The buses.co.uk site has an API, which I used to scrape all of the bus stops in Brighton. I chose to scrape the update data from the website itself though, as I couldn't get the API to give me the combined services for each stop (so you can see when the next 7 and 81 are coming for example). There are typically two stops for each name - one either side of the road. The updates are for both directions, so I actually merged the stops together - which makes searching easier.

All of the stop data was stored in a single SQLite database - mostly for convenience. The database is used in a read-only method in the actual running app. I'm sure I could use something a bit better, particularly when it comes to the distance queries. Currently, as there are a relatively small number of stops I'm using a brute force method to find the nearest stops, but I'm sure with the use of a proper spatial index this could be done a lot more efficiently. If this was scaled up to work for all stops in the UK, then that would be a necessity.

Geo-location

Initially the geo-location was pretty straightforward - I simply used the getCurrentPosition function and I got a latitude and longitude back. Testing in Firefox and Safari on the mac gave fairly reasonable results. However on the iPhone itself I started to notice it wasn't very accurate. Sometimes it was out by several 100 metres, meaning that stops I was standing next to were sometimes not showing up at all! I had noticed, in the past, that the built-in map app sometimes has a bit of "lag" in getting an accurate position. So I switched to using watchPosition and polling for the position for several seconds. This worked pretty well, as the initial result would only be vaguely in the right place and then after a few seconds the position would become more accurate and the listing of nearby stops would start to look more sensible.

Look and feel

Originally I'd planned to mimic the look and feel of the iPhone's built-in weather app. The app is built around a similar concept - you search for bus stops and add them to a list of stops that you can flick through. I tried to get a nice animated sliding action working, but kept on running into trouble on the device itself. In Safari and Firefox the animation all worked ok, but I suspect there's an issue on the iPhone when you are dragging an area that limits redrawing. In the end I had to ditch the flip/swipe gesture too - interfering with the touch events again seemed to cause some rendering issues on occasion. So instead simply clicking on the left or right of the timetable moves between pages. It's a slightly less complicated way of doing things, so there's less that can go wrong really.


Old Brighton No. 5 Bus

Deployment

As this was a nice simple app (on the back-end at least), I decided to take a little time to create a Fabric file to control deployment. The app is hosted on Webfaction, where I have already setup key-based login for ssh. The app is deployed using mod_wsgi with a daemon process, so that one merely needs to copy over the new code, then "touch" the .wsgi file restart the daemon process. Not exactly tricky to do, but making it a one-button process saves making any mistakes.

To trigger the deployment I run my fab file like this:

fab --user=lilspikey --hosts=lilspikey.webfactional.com deploy:webapps/buses/app/

I actually have that in a shell script, that isn't checked into git, so that the deployment specific username, host name and remote directory aren't checked into source control. If I wasn't using key-based login, I'd need to specify a password and I definitely wouldn't want to check that into git!

Source code

You can get the source code on github in my buses repository.