Geocoding Addresses with HERE in an Android Mobile Application

By Nic Raboy | 11 December 2018

Over the past week or so I’ve been writing about using the HERE Location Services (HLS) within an Android mobile application. I started with a tutorial titled, Getting Started with HERE Maps in an Android Application which focused strictly on the configuration towards displaying a map, and then I wrote a tutorial titled Placing Markers on a Map in Android with HERE which focused on showing markers on that map based on latitude and longitude coordinates. Memorizing latitude and longitude coordinates probably isn’t feasible for most people, so how do you do the same with address information?

We’re going to take a look at using the HERE Geocoder API for Android to convert user defined addresses into positions and then drop markers on the map at those positions.

To get an idea of what we plan to accomplish, take a look at the following animated image:

In the above example, the user will enter a query. The geocoder will find a best match for a position and place it as a marker.

Rather than going through the steps of configuring HERE in your Android project, we’re going to assume you’ve already configured it and are already displaying a map.

Finding the Latitude and Longitude Coordinates to an Address

This particular tutorial is going to focus on two parts, the first being the actual geocoding of addresses to positions. Within your Activity, include the following variables:

Some of the variables will probably already look familiar, but we’ve added an editText for our text field and a marker for the current marker that we have on the map. In this example we’re only going to display one marker.

Let’s break down what is happening in the above method. First we are checking to see if a marker exists. If it does, we need to use it to clear the map to start with a fresh slate.

After clearing the map, we set a baseline position to geocode around. In reality, you’d probably want to use your device location rather than hard code a value. With a baseline, we can take a query string and search based on a range parameter.

When we execute the geocoder, we wait until it completes. When the listener triggers that it is complete, we check for errors and if no errors exist, we can loop through the results and place the marker based on the position.

Not too bad right? Now we need to figure out how to get the query to this dropMarker method.

Listening for User Provided Search Queries

There are many solutions towards getting a query to the geocoder and none of them are wrong. Our solution will be to have a text field for user input and when the enter or done key is pressed, the geocoder starts working.

The above code will bind our variable to the view component and create a keyboard listener. If there is an event for a key press and that key is the enter key, we will call the dropMarker method and pass the content of the input. After calling the dropMarker method, the input will be cleared.

Conclusion

You just saw how to use the HERE Geocoder API within an Android mobile application. What we did was take an address query, defined by the user, and convert it into a latitude and longitude position which was then placed on the map as a marker. Of course more complicated things could come out of a geocoder, but this is just one of many examples.

This particular tutorial didn’t revisit any of the configuration steps of the HERE Location Services in Android. If you are stuck and need help configuring a HERE map in Android, check out my previous tutorial on the subject.

by Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.