A search box on your map, answered by your own server

Every web map gets a search box sooner or later, and the usual ones call a paid geocoding API or the public Nominatim, which forbids autocomplete. The map libraries all have a slot for a geocoder of your own; gridpin-map-search fills it.

New Take addresses in JavaScript Browser Server, Docker Websites, help desks Logistics, field work

What you have

A web map with one of these search controls (checked versions): @maplibre/maplibre-gl-geocoder 1.9.4, @mapbox/mapbox-gl-geocoder 5.1.3, leaflet-control-geocoder 42.0.0, leaflet-geosearch 5.2.1, ol-geocoder 4.3.3, deck.gl 9.4.1. The adapters are the folder integrations/map-search of the repository, with no dependencies. For MapLibre:

map.jsfile
import { fromServer, maplibreGeocoderApi } from "./gridpin-map-search/src/index.js";

const source = fromServer({ endpoint: "http://127.0.0.1:8080" });
map.addControl(new MaplibreGeocoder(maplibreGeocoderApi(source), { maplibregl, showResultsWhileTyping: true }));

Steps

1. Start the server on the country file:

terminalCLI
$ gridpin serve serbia.bin --port 8080

2. As the person types, the control asks for suggestions:

terminalCLI
$ curl -s 'http://127.0.0.1:8080/v1/suggest?q=Kneza%20Mihaila%201&k=3'
{"suggestions":[{"commune":"Kragujevac Iii","label":"Kneza Mihaila 1, Kragujevac Iii","lat":44.0080939,"lon":20.9107402,"precision":"house","street":"Kneza Mihaila"},{"commune":"Loznica","label":"Kneza Mihaila 1, Loznica","lat":44.5279646,"lon":19.2224474,"precision":"house","street":"Kneza Mihaila"},{"commune":"Nova Pazova","label":"Kneza Mihaila 1, Nova Pazova","lat":44.9449752,"lon":20.2194093,"precision":"house","street":"Kneza Mihaila"}]}

3. On Enter, or with a full address, it asks the geocoder:

terminalCLI
$ curl -s 'http://127.0.0.1:8080/v1/geocode?q=Kneza%20Mihaila%2010%2C%20Beograd&k=1'
{"results":[{"commune":"Stari Grad","confidence":0.9399999976158142,"flags":["street_exact","commune_exact","house_rep"],"housenumber":"10","lat":44.8161152,"lon":20.4591642,"match_level":"PointAddress","match_status":"M","postcode":"","precision":"house","score":5.5504302978515625,"street":"Kneza Mihaila","weak":false}]}

What you get

The list under the search box and a marker on the chosen address. An answer the engine calls weak is labelled “(approximate)” and comes after the others; a server that does not answer is an error in the control, never an empty list.

Why it works

Each adapter turns GridPin's answers into the shape its library expects: Carmen features for MapLibre, results for Leaflet and OpenLayers, a point for deck.gl. Without a server, ?sheet= loads the country file into the page with gridpin-wasm and every search stays in the browser: a 32 MB country file answered a search in about 3 ms there.

Limits

  • Checked on the versions above; another major version may change a library's slot.
  • deck.gl's widget can only fly to a point, so it gets no weak answer.
  • Without a server the page holds the country file in memory: 32 MB for Serbia.
  • Not on npm yet: take the folder from the repository.

The function: search on a web map. Related: Mapbox search on your server, folium, an address field.