Android navigation app, part 1 — Explanation and geocoding.

Hi, this text is supposed to be introduction to longer series describing my approach to make simple navigation app. In the first part I will describe, or at least try, how to use geocoding and hopefully it will end up with something which will convert map click into address. This is not supposed to be totally basic, I expect You to understand or google something easy to find and concentrate on more general, “architechural” stuff and building a bit longer project from scratch, to something worth (?) publishing.

If anyone will read this or at least I will find some fun in writing this I will write another parts of this… Promises are useful :)

Creating project …

This part should explain how to start new project and add dependencies, etc. Unfortunately I’m too lazy and this is explained everywhere, just start with empty map activity. Rest I will skip this and just explain how I hope this tutorial will look. So…

As this part is mainly created because I want to write something and don’t want to wait until I finish code to some reasonable level. Unfortunately, something fucked up with Butterknife in latest release, so I will solve it later and now use just findViewById(). Similar issue I had with Retrolambda, this will also be in part 2, I will also try to explain how to add this and and basics of its usage.

In part 2 I’m pretty sure we will add localization and new feature — instead of clicking on map we will be able to enter address as string instead of clicking of map. We will also add RxJava and perhaps it’s gonna be enough. Let’s code ;)

XMLs, layout and values.

Created automatically by Android Studio, with few changes.

This is our only layout (for now :P), relative with MapFragment and EditText below it. It would be much better (perhaps) to use Linear, but I’m not sure how it will behave while animated, which will happen sooner or later (it’s rather simple so maybe in next part?). Because of layout_above, we can still set map height to match_parent and don’t care, even animations work and keep ratios :).

By the way it’s cool idea to name colors, dimens and strings according to some fixed scheme.

This is way we are using at work, and at least for me it’s making the code more clear —” maps.address.margin” is much more meaningful than 16dp. This approach is good for dimens, strings, colors and even views. We get better organization and also we don’t have to care about item name. In layout above it was accidentally, I will change it in part 2… Thank’s God for Part 2.

Last thing we have to care about in XMLs is Google Maps api key, grab a link and obtain one:

In case, You haven’t done this before, choose this:

Java, activity and geocoder’s wrapper

In this part we will have just two classes - activity and LocationHelper which will convert onMapClicks into string for our editText (It’s good idea, to set it inactive ;)). Ok, let’s code…

It’s useful to learn how to group lines according to the meaning, like variables which You can find similar somehow, we keep them together and separate with single space from another “group”.

I hope everything above is clear, LocationHelper has listener interface which passes result of geocoding. Really worth remembering is to ALWAYS call geocoder on background thread. Otherwise, we will block UI thread and, well, it’s quite unprofessional.

Last (but not least) is activity, but there is nothing worth mentioning here so just rewrite or copy/paste code below and compile.

Finish and conclusion.

Now, you should have working application which displays street name (or some other part of address) after clicking on map.

In next part, we will add some cool libraries, location tracking and converting entered address string into at least several possible addresses (Seriously, I can’t get better name now). Have fun while compulsively clicking on map anywhere and checking addresses ;)