Android Wrapper for the what3words Web API

An Android library to use the what3words REST API.

The Android API wrapper is useful for Android developers who wish to seamlessly integrate the what3words API into their Android applications, without the hassle of having to manage the low level API calls themselves.

The what3words API is a fast, simple interface which allows you to convert 3 word addresses such as ///index.home.raft to latitude and longitude coordinates such as (-0.203586, 51.521251) and vice versa. It features a powerful autosuggest function, which can validate and autocorrect user input and limit it to certain geographic areas (this powers the search box on our map site). It allows you to request a section of the what3words grid (which can be requested as GeoJSON for easy display on online maps), and to request the list of all languages supported by what3words. For advanced users, autosuggest can be used to post-process voice output. See links on the left to navigate.

All coordinates are latitude,longitude pairs in standard WGS-84 (as commonly used worldwide in GPS systems). All latitudes must be in the range of -90 to 90 (inclusive).

Step 1: Obtain an API key

Sign up to obtain your free API key.

Step 2: Installation

The library is available through Maven Central. Please add the following to your gradle.build.

repositories {
    mavenCentral()
}

dependencies {
    implementation('com.what3words:w3w-android-wrapper:3.0.0')
}

Step 3: Setup

Instantiate an instance of What3WordsV3, from which all API requests can be made

// For all requests a what3words API key is needed
What3WordsV3 api = new What3WordsV3("what3words-api-key", getApplicationContext());

Step 4: Usage

Convert to 3 word address

Convert coordinates, expressed as latitude and longitude to a 3 word address.

More info about convertTo3wa, including returned results is available in the what3words REST API documentation.

Code Example
// Convert coordinates to a 3 word address
ConvertTo3WA words = api.convertTo3wa(new Coordinates(51.508344, -0.12549900))
        .language("en")
        .execute();
System.out.println("Words: " + words);

Convert to coordinates

Convert a 3 word address to a position, expressed as coordinates of latitude and longitude.

This function takes the words parameter as a string of 3 words 'table.book.chair'

More info about convertToCoordinates, including returned results is available in the what3words REST API documentation.

Code Example
// Convert a 3 word address to coordinates
ConvertToCoordinates coordinates = api.convertToCoordinates("filled.count.soap")
        .execute();
System.out.println("Coordinates: " + coordinates);

Autosuggest

When presented with a 3 words address which may be incorrectly entered, autosuggest returns a list of potential correct 3 word addresses. It needs the first two words plus at least the first character of the third word to produce suggestions.

This method provides corrections for mis-typed words (including plural VS singular), and words being in the wrong order.

Optionally, clipping can narrow down the possibilities, and limit results to:

  • one or more countries
  • a geographic area (a circle, box or polygon)

This dramatically improves results, so we recommend that you use clipping if possible.

To improve results even further, set the focus to user’s current location. This will make autosuggest return results which are closer to the user.

More info about autosuggest, including returned results is available in the what3words REST API documentation.

Code Example Simple basic call.
Autosuggest autosuggest = api.autosuggest("fun.with.code")
        .execute();
System.out.println("Autosuggest: " + autosuggest);
Code Example Autosuggest, clipping the results returned to France and Germany.
Autosuggest autosuggest = api.autosuggest("fun.with.code")
        .clipToCountry("FR", "DE")
        .execute();
System.out.println("Autosuggest: " + autosuggest);
Code Example Focus on (51.4243877,-0.34745).
Autosuggest autosuggest = api.autosuggest("fun.with.code")
        .focus(new Coordinates(51.4243877,-0.34745))
        .execute();
System.out.println("Autosuggest: " + autosuggest);

Grid Section

Grid Section returns a section of the what3words 3m x 3m grid as a set of horizontal and vertical lines covering the requested area, which can then be drawn onto a map.

The requested box must not exceed 4km from corner to corner, or a BadBoundingBoxTooBig error will be returned.

More info about gridSection, including returned results is available in the what3words REST API documentation.

Code Example
// Obtain a grid section within the provided bounding box
GridSection gridSection = api.gridSection(
        new BoundingBox(
            new Coordinates(51.515900, -0.212517), 
            new Coordinates(51.527649, -0.191746)
        )
    ).execute();
System.out.println("Grid section: " + gridSection);

Available Languages

This function returns the currently supported languages. It will return the two letter code, and the name of the language both in that language and in English.

More info about availableLanguages, including returned results is available in the what3words REST API documentation.

Code Example
// Obtain the list of available languages
AvailableLanguages languages = api.availableLanguages().execute();
System.out.println("Languages: " + languages);

Handling Errors

For each invocation of an exectute() method, one of the following response objects is returned: ConvertTo3WA, ConvertToCoordinates, Autosuggest, GridSection or AvailableLanguages. Each of these responses has an isSuccessful() method. If this method returns false it can be determined than an error occurred.

In this case, a call to getError() can be made retrieve an enum type representing the error response from the API. All error enums are contained within the Error

Code Example
Autosuggest autosuggest = api.autosuggest("freshen.overlook.clo")
    .clipToCountry("fr")
    .execute();

if (autosuggest.isSuccessful()) {
    ...
} else { // An error occurred
    What3WordsError error = autosuggest.getError();

    if (error == What3WordsError.BAD_CLIP_TO_COUNTRY) { // Invalid country clip is provided
        System.out.println("BadClipToCountry: " + error.getMessage());
    }
}

Step 5: Putting It All Together

The example below takes the concepts described above, and turns some of them into a complete example. Here we take a partial 3 word address and pass it into Autosuggest - clipping the results to consider only addresses in France, setting a focus of Paris, and returning a single result

We then take the result and convert the 3 word address to coordinates, and find the nearest place.

Main Thread Loop

Because Android does not allow networking operations on the main application thread, API calls need to be made in a background thread. It is recomended to use IntentService functionality to perform asynchronous requests in a production application, however, for simple demonstration purposes API requests can be made in a simple child thread.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.what3words.androidwrapper.What3WordsV3;
import com.what3words.javawrapper.response.APIResponse.What3WordsError;
import com.what3words.javawrapper.response.ConvertToCoordinates;
import com.what3words.javawrapper.response.Autosuggest;
import com.what3words.javawrapper.request.Coordinates;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Thread(new Runnable() {
            public void run() {
                // For all requests a what3words API key is needed
                What3WordsV3 api = new What3WordsV3("what3words-api-key", MainActivity.this);

                Autosuggest autosuggest = api.autosuggest("freshen.overlook.clo")
                        .clipToCountry("FR")
                        .focus(new Coordinates(48.856618, 2.3522411))
                        .nResults(1)
                        .execute();

                if (autosuggest.isSuccessful()) {
                    String words = autosuggest.getSuggestions().get(0).getWords();
                    System.out.printf("Top 3 word address match: %s%n", words);

                    ConvertToCoordinates convertToCoordinates = api.convertToCoordinates(words).execute();
                    if (convertToCoordinates.isSuccessful()) {
                        System.out.printf("WGS84 Coordinates: %f, %f%n",
                                convertToCoordinates.getCoordinates().getLat(),
                                convertToCoordinates.getCoordinates().getLng());
                        System.out.printf("Nearest Place: %s%n", convertToCoordinates.getNearestPlace());
                    } else {
                        What3WordsError error = autosuggest.getError();
                        if (error == What3WordsError.INTERNAL_SERVER_ERROR) { // Server Error
                            System.out.println("InternalServerError: " + error.getMessage());

                        } else if (error == What3WordsError.NETWORK_ERROR) { // Network Error
                            System.out.println("NetworkError: " + error.getMessage());

                        }
                    }
                } else {
                    What3WordsError error = autosuggest.getError();

                    if (error == What3WordsError.BAD_CLIP_TO_COUNTRY) { // Invalid country clip is provided
                        System.out.println("BadClipToCountry: " + error.getMessage());

                    } else if (error == What3WordsError.BAD_FOCUS) { // Invalid focus
                        System.out.println("BadFocus: " + error.getMessage());

                    } else if (error == What3WordsError.BAD_N_RESULTS) { // Invalid number of results
                        System.out.println("BadNResults: " + error.getMessage());

                    } else if (error == What3WordsError.INTERNAL_SERVER_ERROR) { // Server Error
                        System.out.println("InternalServerError: " + error.getMessage());

                    } else if (error == What3WordsError.NETWORK_ERROR) { // Network Error
                        System.out.println("NetworkError: " + error.getMessage());

                    } else {
                        System.out.println(error + ": " + error.getMessage());

                    }
                }
            }
        }).start();
    }
}

Step 6: Further Reading

Complete examples of each endpoint are also available from within the Java Wrapper: -