Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class NominatimGeocoder

  1. Object
  2. NominatimGeocoder

A geocoder speaking Nominatim’s JSON API: /search?q= for a forward search and /reverse?lat=&lon= for the address at a point.

Point it at your own server. A Codename One backend answers this API from an OpenStreetMap extract with com.codename1.backend.osm.OsmGeocoding:

NominatimGeocoder geocoder = new NominatimGeocoder("https://api.example.com/geocode");
geocoder.search("main street 12", 5, new GeocodeCallback() {
    public void placesFound(List places) {
        Place best = (Place) places.get(0);
        Log.p(best.getDisplayName() + " at " + best.getLocation());
    }
    public void geocodeFailed(String message, Throwable error) {
        Log.p(message);
    }
});

Before you ship: the default endpoint is the OpenStreetMap Foundation’s public Nominatim server, which its usage policy limits to one request per second per application, forbids for search-as-you-type, and may block without notice (https://operations.osmfoundation.org/policies/nominatim/). It is there for development; an application with real users needs its own server.

Every result is OpenStreetMap data and must be credited to “OpenStreetMap contributors” where the user sees it.

Fields

public static final String PUBLIC_BASE_URL = "https://nominatim.openstreetmap.org"The OpenStreetMap Foundation’s public Nominatim server, used when no other base URL is configured.

Constructors

public NominatimGeocoder()Creates a geocoder pointing at the public Nominatim server.
public NominatimGeocoder(String baseUrl)Creates a geocoder pointing at a Nominatim-compatible endpoint.

Methods

public String getBaseUrl()The endpoint requests go to, without a trailing slash.
public NominatimGeocoder setBaseUrl(String baseUrl)Points this geocoder at a Nominatim-compatible endpoint.
public void search(String query, int limit, GeocodeCallback callback)Finds places matching a name or an address, best first.
public void reverse(LatLng location, GeocodeCallback callback)Finds the address or place at a point.
public static List parseSearch(String json) throws IOExceptionParses a Nominatim /search response, an array of places.
public static List parseReverse(String json) throws IOExceptionParses a Nominatim /reverse response, one place or an error object.

Inherited methods

Field details

PUBLIC_BASE_URL

public static final String PUBLIC_BASE_URL = "https://nominatim.openstreetmap.org"
The OpenStreetMap Foundation’s public Nominatim server, used when no other base URL is configured. For development only – see the class documentation.

Constructor details

NominatimGeocoder

public NominatimGeocoder()
Creates a geocoder pointing at the public Nominatim server.

NominatimGeocoder

public NominatimGeocoder(String baseUrl)
Creates a geocoder pointing at a Nominatim-compatible endpoint.

Parameters

baseUrl String
for example https://api.example.com/geocode

Method details

getBaseUrl

public String getBaseUrl()
The endpoint requests go to, without a trailing slash.

setBaseUrl

public NominatimGeocoder setBaseUrl(String baseUrl)
Points this geocoder at a Nominatim-compatible endpoint. A null or empty value restores PUBLIC_BASE_URL; a trailing slash is trimmed.

Parameters

baseUrl String
the endpoint

Returns

this geocoder

search

public void search(String query, int limit, GeocodeCallback callback)
Finds places matching a name or an address, best first.

Parameters

query String
free text, a place name or an address
limit int
the most results wanted, 1 to 50
callback GeocodeCallback
receives the outcome on the event dispatch thread

reverse

public void reverse(LatLng location, GeocodeCallback callback)
Finds the address or place at a point. The callback receives a list of at most one place, empty when the service knows nothing near it.

Parameters

location LatLng
the point
callback GeocodeCallback
receives the outcome on the event dispatch thread

parseSearch

public static List parseSearch(String json) throws IOException
Parses a Nominatim /search response, an array of places.

Parameters

json String
the response body

Returns

the Place results in the order given, possibly empty

Throws

IOException
when the body is not a Nominatim result array

parseReverse

public static List parseReverse(String json) throws IOException
Parses a Nominatim /reverse response, one place or an error object.

Parameters

json String
the response body

Returns

a list holding the place, or empty when the service found none

Throws

IOException
when the body is not a Nominatim result