Class NominatimGeocoder

java.lang.Object
com.codename1.maps.geocoding.NominatimGeocoder

public class NominatimGeocoder extends Object

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.

  • Field Details

    • PUBLIC_BASE_URL

      public static final String PUBLIC_BASE_URL
      The OpenStreetMap Foundation's public Nominatim server, used when no other base URL is configured. For development only -- see the class documentation.
      See Also:
  • 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: 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: 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: free text, a place name or an address
      • limit: the most results wanted, 1 to 50
      • callback: 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: the point
      • callback: 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: the response body
      Returns

      the Place results in the order given, possibly empty

      Throws
      • IOException: when the body is not a Nominatim result array
      Throws:
      IOException
    • parseReverse

      public static List parseReverse(String json) throws IOException

      Parses a Nominatim /reverse response, one place or an error object.

      Parameters
      • json: 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
      Throws:
      IOException