Getting an address from latitude and longitude

Sometimes, you’ll need to get one or a list of nearby addresses using latitude and longitude. Unfortunately, the standard way of implementing this feature has a bug within Google’s Android ecosystem, making the implementation unreliable. I’ll go over an alternative implementation that is more reliable.

Let’s say that you want to retrieve an address using the latitude and longitude that you get from your phone’s GPS system. The standard documented way of going about this is using the Geocoder library, pass in your latitude and longitude data, and ask for a maximum number of suggested addresses. The code usually looks something like this.

This is the ideal and clean way to retrieve addresses using geolocation data, but the results you get will be inconsistent. More often than not, you won’t get any addresses back and you’ll get an error in your console that goes something like Geocoder throwing exception: IOException: Service not Available.

If you look at the Geocoder docs, it has a line that says

The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.

The question is… what the heck is a “backend service” mean? Just like many of Google’s lovely documentation, it fails to mention what this backend service is. Is it something provided by Google or is it something you have to implement on your server side code? At the time of this writing, I still don’t know the answer.

Many of the solutions to this problem found on StackOverflow suggests that you simply reboot your device. This seems to work for some people (it never worked for me), but this is an unreasonable request to ask of to your users. Thus, I came up with something way more roundabout to get the addresses but way more reliable at the same time. Rather than using the Geocoder library, I make http requests directly to Google Maps API with the latitude and longitude interpolated in the web url. I get a JSON object back from this request and I parse through that JSON object to get the address information that I need.

The code sample below assumes that you’re using OkHttp version 3 to make your HTTP requests and Google’s gson library to parse the JSON, but you can use whatever you want.

 

As you can see, the code is straightforward. I build a url string that has my latitude and longitude information in it, and I make a HTTP request to that url, and then I parse through the JSON object that I get back appropriately to get the address that I want. The nice part of the JSON object that we do get back is that it comes with a field with the address already properly formatted for us 🙂

There are other “workarounds” that utilizes Geocoder floating around the internets (some of which include trying to make requests with Geocoder in an endless while loop) and using Geocoder ends up in cleaner code, I think that the brittleness of that library makes it a suboptimal solution. My workaround makes you write a bit more custom code to get the address data that you need, but it’s reliable and will work consistently.

About the Author Chris Jeon

Software developer currently focusing on Android development.