Click here to Skip to main content
13,863,336 members
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 29 Jan 2019
Licenced CPOL

Reverse Geocoding Made Easy: Getting Location Details with the TomTom Android SDK

, 29 Jan 2019
In this article, we’re going to explore the capabilities available within the new TomTom SDKs.

Editorial Note

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Geocodes -- which mean GPS coordinates -- are great if you’re a computer and can easily calculate exactly where they map to. If you’re a humble human being, however, working with geocodes is tough, because humans don’t think or talk about locations in terms of geocodes. Instead, we use street names, addresses and landmarks to determine where things are.

This is why reverse geocoding is such an important feature for bridging the gap between computers and humans. Reverse geocoding allows applications to convert geocodes into street other human-readable location information. Reverse geocoding thus makes it possible to use the precision of GPS and geocodes for tracking locations within an application, while still presenting information to human users in ways that humans can easily interpret.

The tricky thing about reverse geocoding, however, is that there has not traditionally been a simple way to leverage it within an application. Unlike, say, IP addresses, which you can easily translate into domain names using a DNS service, there is no standardized service for translating geocodes into human-readable location information.

Fortunately, TomTom has made things much easier for developers in this respect by adding reverse geocoding to its SDKs. The feature makes it quite easy to implement reverse geocoding into an app using simple API calls.

To provide a sense of how reverse geocoding works and how developers can easily take advantage of it with the help of TomTom’s SDKs, this article walks through a scenario involving the display of location data in an Android mobile app. We’ll start by discussing what you need to be able to use the SDK; then we’ll create our app to determine the location of the device, find out more information about the location using the TomTom SDK, and then display the results on the screen.

In this article, we’re going to explore the capabilities available within the new TomTom SDKs. The example we’ll be building in this article uses the TomTom SDK for Android. The same functionality is available with the Web and iOS SDKs. You can also use the TomTom APIs directly from your applications. You can follow links to the IOS and Web SDKs below.

The TomTom SDKs

The TomTom Developer Portal is where you’ll find everything you need to get up and running with the TomTom APIs. The first thing you’ll want to do is create an account on the portal. From the home page, enter your email address and click on the “Get a free API key” button.

Fig. 1 The TomTom Developer Portal

The next step in the process is to select a username and read through the terms and conditions. Your free account supports up to 2,500 free transactions per day. If 2,500 API transactions per day aren’t enough, you can purchase more transactions by visiting the My Credits screen in the developer dashboard.

An email contains a link to set the password for your account, and with that, you’ll be ready to go. You need to configure an application before you can request an API Key. From your dashboard, click on the + Add an App button.

Applications require a name, and you’ll need to enable the products which the application needs to access. For this example, the product we’ll be using is the Search API product. If you’re following along, select the Search API product and click on Create App.

Fig. 2 Creating a New Application

You can receive a unique API key by visiting the TomTom Developer Portal, and entering your email address for a “Free API Key.” You’ll need to read and agree to the terms and conditions and select a username. An email prompts you to set a password, and from there, you can navigate to your dashboard and create a new application.

Applications are approved quickly and appear on your dashboard. The entry shows the Consumer API Key and information about your application, including the approval status of the application, and each of the products it uses.

Fig. 3 Application Details, Including the API Key

We’ll need this API as we add the TomTom SDK to our project and use it to get detailed information about our location.

Setting Up Our Project

I used Android Studio to create this project and began with a blank activity screen. Opening up activity_main.xml, I added a button (btnGetInformation) to trigger the information request, and then a group of text fields below to display information about the location. We’ll include fields for:

  • The Street Address - txtStreet
  • City - txtCity
  • Zip Code - txtZipCode
  • State - txtState

Fig. 4 Layout for Our App

Now let’s open, connect to each of these controls, and we can add an EventListener to the button, and connect to the device’s Location Manager.

public class MainActivity extends AppCompatActivity {

    private Button btnInformation;
    private TextView txtStreet;
    private TextView txtCity;
    private TextView txtState;
    private TextView txtZipCode;

    protected void onCreate(Bundle savedInstanceState) {

        btnInformation = findViewById(;
        txtStreet = findViewById(;
        txtCity = findViewById(;
        txtZipCode = findViewById(;
        txtState = findViewById(;
Fig. 5 Initializing the Controls for the Main Activity (

Capturing Location Information from the Device

We’re going to set our application to get location updates from the GPS Location Service on the device. Before we can do this, we need to add a uses-permission entry in the AndroidManifest.xml file.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Fig. 6 Adding Permissions for the Location Service

Next, we’ll add a LocationManager and a LocationListener to the file.

private LocationManager locationManager;
private LocationListener locationListener;

Now, we’ll initialize them inside the onCreate method. The LocationListener has four methods we need to implement. We’ll just be implementing the onLocationChanged method later in this project.

locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {

    public void onStatusChanged(String provider, int status, Bundle extras){

    public void onProviderEnabled(String provider) {

    public void onProviderDisabled(String provider) {
Fig. 7 Implementing the LocationManager and LocationListener

Now we need to enable the LocationManager and ensure that the user has granted permissions for us to access the Location Service. We’ll check for permissions in the onCreate method, and we’ll also add the check to the overridden onRequestPermissionResult method. The command that we execute to start receiving updates is:


The parameters specify:

  • The source of the location (GPS provider in our case)
  • The minimum time between updates (we selected 5000 milliseconds or 5 seconds)
  • The minimum distance between updates (we selected 1 meter)
  • The LocationListener object.

We’ll check for permissions in onCreate:

if (ActivityCompat.checkSelfPermission(
    ) != PackageManager.PERMISSION_GRANTED
         && ActivityCompat.checkSelfPermission(
         ) != PackageManager.PERMISSION_GRANTED) {
                 new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
} else {
Fig. 8 Checking Permissions in the onCreate Method

We’ll also add the permissions check to the onRequestPermissionsResult method.

public void onRequestPermissionsResult(
    int requestCode, 
    @NonNull String[] permissions, 
    @NonNull int[] grantResults) {

        if (grantResults.length > 0 
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (ContextCompat.checkSelfPermission(
                Manifest.permission.ACCESS_FINE_LOCATION) ==             
                PackageManager.PERMISSION_GRANTED) {                      
Fig. 9 Checking Permissions in the onRequestPermissionsResult Method

So, now whether our user has already granted permissions for the app to use the location of the device, or they give permission when opening the app, the app can now access the location. Let’s add one more thing before we continue. We’ll initialize a variable called currentLocation of type Location, and we’ll set this in the onLocationChanged method of the LocationListener.

private Location currentLocation;

We’ll set currentLocation whenever the Location Service broadcasts a change.

public void onLocationChanged(Location location) {
    currentLocation = location;
Fig. 10 Setting the currentLocation property inside the LocationListener

Let’s add the TomTom SDK so we can take the current location and request more information about it to complete our app.

Adding TomTom Support

We need to add the TomTom SDK as a dependency of our project. The first step is to add the TomTom repository to our build.gradle file. We want the Gradle build file for the project, and we need to locate the allprojects object within the file. Add the TomTom repository to this object.

allprojects {
    repositories {
        maven {
            url ""
Fig. 11 Adding the TomTom Repository

Now that we can access the repository, open the Gradle build file for the app, and add the following dependencies to the dependencies object.

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation ''
    implementation ''
    implementation ''
    testImplementation 'junit:junit:4.12'
    androidTestImplementation ''
    androidTestImplementation ''
Fig. 12 Adding the TomTom Search SDK

You may need to refresh the Gradle libraries for your project first, but these changes add TomTom search functionality to your project. The final step is to authorize API access with the API Key we created through the TomTom Developer Portal.

Open the AndroidManifest.xml file, and add the following meta-data object after the last activity object and before the application object is closed. Ensure that you replace ReplaceWithAPIKey with your API Key.



    android:value="ReplaceWithAPIKey" />
Fig. 13 Adding the API Key for TomTom Search SDK

Reverse Geocoding the Location

The first thing we’re going to do is implement a couple of methods to configure the API and allow building a query. Add the following functions to the file. We’ll pass in the Application Context, which is how the API is configured with our API Key.

protected SearchApi createSearchAPI() {
    SearchApi searchApi = OnlineSearchApi.create(getApplicationContext());
    return searchApi;
Fig. 14 Function to Initialize the SearchAPI

The next function constructs a search query for us. We accept the latitude and longitude values and return a query object.

protected ReverseGeocoderSearchQuery createReverseGeocoderQuery(
    double latitude, 
    double longitude) {
        return ReverseGeocoderSearchQueryBuilder
                .create(latitude, longitude)
Fig. 15 Function to Build our ReverseGeocoderQuery Object

Finally, let’s build our reverseGeocode function, which configures the connection to the API, builds the query, and then handles the results.

protected void reverseGeocode(
    final double latitude, 
    final double longitude) {
        SearchApi searchAPI = createSearchAPI();
        ReverseGeocoderSearchQuery reverseGeocoderQuery =
            createReverseGeocoderQuery(latitude, longitude);
            reverseGeocoderQuery, new RevGeoSearchResultListener() {
                public void onSearchResult(ReverseGeocoderSearchResponse   response) {
                    List<ReverseGeocoderFullAddress> addresses =   
                    if (addresses.size() > 0) {
                        Address address = addresses.get(0).getAddress();
                        txtStreet.setText(address.getStreetNumber() + ' ' + address.getStreetName());

                public void onSearchError(SearchError error) {
                    Log.d("Address: ",  getApplicationContext().getString(R.string.reverse_geocoding_error));
Fig. 15 Function to Build our ReverseGeocoderQuery Object

The last thing we need to do is connect the button to run the reverseGeocode function, using the currentLocation. Add the following to the end of the onCreate function.

btnGetInformation.setOnClickListener(v ->
Fig. 16 Adding a Click Listener to the Button

Running the Application

You can run your app in an emulator, or install it on an Android device. I tested mine with a Pixel 2 Emulator running version 28 of the Android API. I manipulated the coordinates to 45.512046, -122.627833, and then clicked on the GET LOCATION INFORMATION button.

Fig. 17 The Completed App in Action

Additional Information

If you would like to learn more about options available within the TomTom APIs, you can access the online documentation or use the API Explorer to try different parameters and see how they all work together.

If you would like to download the code for this example, you can find it here. You’ll need to add your API key in the AndroidManifest file before you can run the application.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mike Mackrory
United States United States
Mike Mackrory is a Global citizen who has settled down in the Pacific Northwest - for now. By day he works as a Senior Engineer on a Quality Engineering team and by night he writes, consults on several web based projects and runs a marginally successful eBay sticker business. When he’s not tapping on the keys, he can be found hiking, fishing and exploring both the urban and the rural landscape with his kids. Always happy to help out another developer, he has a definite preference for helping those who bring gifts of gourmet donuts, craft beer and/or Single-malt Scotch.

You may also be interested in...


Comments and Discussions

QuestionNice article Pin
David Crow31-Jan-19 7:27
memberDavid Crow31-Jan-19 7:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190214.1 | Last Updated 29 Jan 2019
Article Copyright 2019 by Mike Mackrory
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid