Class 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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe OpenStreetMap Foundation's public Nominatim server, used when no other base URL is configured. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a geocoder pointing at the public Nominatim server.NominatimGeocoder(String baseUrl) Creates a geocoder pointing at a Nominatim-compatible endpoint. -
Method Summary
Modifier and TypeMethodDescriptionThe endpoint requests go to, without a trailing slash.static ListparseReverse(String json) Parses a Nominatim/reverseresponse, one place or an error object.static ListparseSearch(String json) Parses a Nominatim/searchresponse, an array of places.voidreverse(LatLng location, GeocodeCallback callback) Finds the address or place at a point.voidsearch(String query, int limit, GeocodeCallback callback) Finds places matching a name or an address, best first.setBaseUrl(String baseUrl) Points this geocoder at a Nominatim-compatible endpoint.
-
Field Details
-
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
Creates a geocoder pointing at a Nominatim-compatible endpoint.
Parameters
baseUrl: for examplehttps://api.example.com/geocode
-
-
Method Details
-
getBaseUrl
The endpoint requests go to, without a trailing slash. -
setBaseUrl
Points this geocoder at a Nominatim-compatible endpoint. A
nullor empty value restoresPUBLIC_BASE_URL; a trailing slash is trimmed.Parameters
baseUrl: the endpoint
Returns
this geocoder
-
search
Finds places matching a name or an address, best first.
Parameters
query: free text, a place name or an addresslimit: the most results wanted, 1 to 50callback: receives the outcome on the event dispatch thread
-
reverse
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 pointcallback: receives the outcome on the event dispatch thread
-
parseSearch
Parses a Nominatim
/searchresponse, an array of places.Parameters
json: the response body
Returns
the
Placeresults in the order given, possibly emptyThrows
IOException: when the body is not a Nominatim result array
- Throws:
IOException
-
parseReverse
Parses a Nominatim
/reverseresponse, 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
-