Hands On

Geocoding Addresses with HERE in an Android Mobile Application

By Nic Raboy | 11 December 2018

Try HERE Maps

Create a free API key to build location-aware apps and services.

Get Started

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:

private Map map = null;
private MapFragment mapFragment = null;
private EditText editText = null;
private MapMarker marker;

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.

Within the same Activity, include the following dropMarker method:

public void dropMarker(String query) {
    if(marker != null) {
    GeoCoordinate tracy = new GeoCoordinate( 37.7397,-121.4252);
    GeocodeRequest2 request = new GeocodeRequest2(query).setSearchArea(tracy, 5000);
    request.execute(new ResultListener<List<GeocodeResult>>() {
        public void onCompleted(List<GeocodeResult> results, ErrorCode error) {
            if (error != ErrorCode.NONE) {
                Log.e("HERE", error.toString());
            } else {
                for (GeocodeResult result : results) {
                    marker = new MapMarker();
                    marker.setCoordinate(new GeoCoordinate(result.getLocation().getCoordinate().getLatitude(), result.getLocation().getCoordinate().getLongitude(), 0.0));

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.

Before we look at the Java logic, we need to update the XML layout:

    android:orientation="vertical" >




Take note that our EditText has an id of query which will be used within the Java file. Inside the onCreate method, where we have the map initialization, we need to add the following:

editText = (EditText) findViewById(R.id.query);

editText.setOnKeyListener(new View.OnKeyListener() {
    public boolean onKey(View view, int keyCode, KeyEvent keyevent) {
        if ((keyevent.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
            return true;
        return false;

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.


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.