Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class NominatimGeocoder
- Object
- 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 IOException | Parses a Nominatim /search response, an array of places. |
public static List parseReverse(String json)
throws IOException | Parses 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"Constructor details
NominatimGeocoder
public NominatimGeocoder()NominatimGeocoder
public NominatimGeocoder(String baseUrl)Parameters
baseUrlString- for example
https://api.example.com/geocode
Method details
getBaseUrl
public String getBaseUrl()setBaseUrl
public NominatimGeocoder setBaseUrl(String baseUrl)null or
empty value restores PUBLIC_BASE_URL; a trailing slash is trimmed.Parameters
baseUrlString- the endpoint
Returns
search
public void search(String query, int limit, GeocodeCallback callback)Parameters
queryString- free text, a place name or an address
limitint- the most results wanted, 1 to 50
callbackGeocodeCallback- receives the outcome on the event dispatch thread
reverse
public void reverse(LatLng location, GeocodeCallback callback)Parameters
locationLatLng- the point
callbackGeocodeCallback- receives the outcome on the event dispatch thread
parseSearch
public static List parseSearch(String json)
throws IOException/search response, an array of places.Parameters
jsonString- the response body
Returns
Place results in the order given, possibly emptyThrows
IOException- when the body is not a Nominatim result array
parseReverse
public static List parseReverse(String json)
throws IOException/reverse response, one place or an error object.Parameters
jsonString- the response body
Returns
Throws
IOException- when the body is not a Nominatim result