MBTA Bus Locator

I wrote an app for my G1 to get information from the real-time MBTA bus location feed and display it in Google Maps as bus icons. It was an interesting experience to develop for.

The Android eclipse environment has its fair share of hiccups unfortunately, simple things like how a down arrow usually behaves, or not being able to scroll all the way to the bottom of a list using your mouse scrollwheel. The emulator and eclipse’s debugging capabilities were really helpful, and robust for the most part. The most difficult thing in developing for it was figuring out how to do what I wanted to do, what functions and classes to use. I guess that’s inherent in developing for a new platform.

The app is pretty simple. It’s one screen, with most of it taken up by Google Maps and a bar at the top containing space for a refresh button and a status update box. It defaults to Watertown square when it starts, although that could easily be changed to use GPS or something else. When the app starts and when the refresh button is pushed, it retrieves a list of bus locations from the feed XML. Those locations are sorted by distance to get the 20 closest buses to where the center of the map is. (The distance is currently computed by sqrt(latitude^2 + longitude^2). This is slightly incorrect since a difference in latitude may be smaller or larger than a difference in longitude. I’ll fix that soon using this. Overlays are then created using a bus picture I got from Wikipedia and inserted into Google Maps.

Most of the time is spent getting and parsing the XML. This step happens in the background, using the handy AsyncTask class which also provides an easy way to do progress updates from a non-GUI thread. Overall it seems like the data is around 45 seconds to a minute out of date from when it appears on my phone. The feed allows you to specify things like the route number, which might shrink the amount of XML downloaded and parsed. The feed seems to be updated every 30 seconds although I can’t tell for sure, and it asks users not to poll it more often than every 10 seconds. All that probably accounts for the delay, which is good because these are things that could be upgraded in the future without any changes to end user applications.

Since it’s a trial feed most buses don’t have route numbers listed in the XML, and some don’t show up at all. You can get a basic idea of what bus a particular icon is by checking it again after 30 seconds and seeing where it moved to. When they move the feed out of beta (or alpha or whatever it is) they’ll also mention the direction of the bus (inbound or outbound) which should make that guesswork unnecessary.

For me, the most useful part of this is knowing approximately how late a particular bus is. Among other things, it means not having to wait in the cold or rain for a bus that could be 20 minutes away

Related posts: