technology from back to front

Android Postcode app

I’ve been playing around with Android development recently, and one of the apps is now worth sharing with everyone else. It’s not particularly complex, but last I looked, there wasn’t an app to do this: tell me what my postcode is. Given you’re holding a phone that knows where it is, and that can talk to the web where there are various services that will tell you what the postcode is for a given location, why not tie these things together? So, I built a little app that does all of this. It looks like the screenshot to the right, and that QR code you see is the market link to download it, or read on for more details about the development of it.


Screenshot

Android development has turned out to be remarkably nice, especially given I’ve done both a lot of embedded systems work and then tried to get stuff working on my old Symbian phone (short answer: don’t bother, it’s deeply frustrating to work with), and so this was a breath of fresh air. There was a few interesting items along the way however:

1) Location lookup:

First we need a location. You’ll need to cope with the fact that potentially the GPS could be switched off (the Android┬ácompatibility┬ádefinition ensures there will be GPS, but not that it’ll be on), and there could potentially be other location providers, so it’s nice to use those as well if they’re any good. So, we dig through the LocationManager, wake up every provider we can find with getProviders() and tell them to give us some data ASAP. There are probably ways to do this more nicely, but given that we not only need to get accurate data, we need to make sure it’s not that old (which we can’t actually select on unfortunately, but we can ignore incoming data that’s too old), the easiest way is just to tell everyone to give you data, and then switch everything back off again once we’ve got the data. On my Desire, when outside or at least not in the bowels of the Tube, I tend to get location data in a few seconds like this. I also setup a timer for a minute, so if we’ve kept trying for that long and nothing good comes up, we throw up an error message and tell the user to try again later.

2) Postcode lookup

So we got a latitude/longitude, and it’s fairly recent, but now we need that translated into a postcode. Luckily, since the release of the OS OpenData dataset, which can be munged to get lat/long -> postcode, a few web services have popped up that’ll do this for us. They’re not all very reliable though, and given they’re web services, it’s entirely possible they’ll go away or change their API at some point, so we have a list of different services to try, and if anything goes wrong we just skip a service and try the next one. Current order is

  1. What is my postcode?
  2. Geonames “find nearby postal codes”
  3. UK Postcodes

Theoretically all 3 could go down eventually, but that should be enough for a bit. Ordering is based on my rather crude estimates of which ones have been most reliable/best data during my testing of the app. Of course, the phone’s data connection could not work, but again we throw an error and suggest you get the data connection sorted out if this happens.

3) Displaying the text

I started off just putting the text into a simple TextView, but it wasn’t very big and a bit difficult to read at the default font sizes. I could have started fiddling around with the font size and gotten something that would look good on my phone, but given I’d like this to be at least slightly cross-compatible, I ended up writing a revised TextView called AutoScaledTextView that automagically resizes text so it’s as big as possible given the amount of space available for the View (Android’s term for the basic displayable widget). It’s a bit kludgy (given non-fixed width fonts), but it seems to work reasonably well and does achieve the goal of displaying the postcodes nice and big.

Overall, this process has been fairly painless. I spent a bit of time figuring out how to do exactly what I wanted, but for the most part I felt Android was helping me along as opposed to being something I had to wrestle with. Provided you’ve got a phone (I’ve done parts of this before I had the phone, but it’s a bit nastier that way), getting yourself up and running and building pretty complex apps is quite easy, and I’ll be posting about another one of those that reuses the data we’ve gathered here today in another way later on. Code for all of this is on Github as usual, or you can just download it from the Market. Enjoy! I hope to see some reviews/bug reports soon :)

by
Tom Parker
on
26/09/10
  1. Just tried it (on a Nexus One running Android 2.2.1). All it does is say “Unknown location”. My GPS is on and working (at least, according to “GPS Status” by EclipSim). I’m in Seattle, WA, US.

  2. Frank Shearar
    on 27/09/10 at 6:34 am

    Have you written anything for the iPhone before? Any comparisons?

    I maintained an application for the Nokia 9110, and found the experience… not pleasant.

  3. Frank Shearar
    on 27/09/10 at 6:36 am

    @Eric The code’s for UK post codes. (http://www.ordnancesurvey.co.uk/oswebsite/opendata/ doesn’t cover the US, as far as I can tell.)

  4. Worked perfectly for me. A handy addition to the toolbox – thank you.

  5. @Eric: I had considered looking into the non-UK problem, but hadn’t dealt with it properly yet. Geonames at least should be able to get postcodes for that, so I’ll play around with a few locations and see if I can improve it a little…

    @Frank: Nope. I’d have to buy a Mac to do so AFAIK, so it’s not really of interest to me at the moment. Making it hard for devs to play around with your system is annoying. That, and if I did this for the iPhone you wouldn’t be seeing this app for another few weeks while it winds it’s way through Apple’s approval process :)

  6. Lee Wei
    on 12/04/11 at 1:39 pm

    I’ve written a solver for the game “Swap Me” on the iPhone (basically just perform a brute-force search of the game solution space) Hosted on Google Code @ http://code.google.com/p/swapmesolver/

    I’ve also written a client-server app for the Android after that (basically it’s meant to be like a buzzer system ie. 4 clients to a server, the clients just have one big button to tap, and the server records the sequence (up to a maximum of 4 each time) of incoming taps) – unfortunately the source code’s buried somewhere in my Dropbox.

    I may own a MBP, but I’m not really a big fan of Objective-C, find it much easier to code in Java/Eclipse +AndroidSDK. The GUI design seems easier too…

  7. dariusj
    on 05/01/12 at 5:36 pm

    exelent,
    thank you TomP,
    works greate on my GS2

  8. Excellent app.

 
 


nine − 8 =

2000-14 LShift Ltd, 1st Floor, Hoxton Point, 6 Rufus Street, London, N1 6PE, UK+44 (0)20 7729 7060   Contact us