The Mapbox Geocoding API on your own server
Code written against Mapbox Geocoding v5 knows one path and one response shape. Serve both yourself and the searches stop going to Mapbox: no token budget, no per-request bill, no addresses leaving your network.
New Leave a paid API Server, Docker JavaScript Retail, e-commerce Logistics, field work
What you have
A server you can run a program on and the country file. The Mapbox libraries insist on a key of the form pk.<base64>.x before they start; the server does not check it, so any such string does.
Steps
1. Start the server:
$ gridpin serve serbia.bin --port 8080
2. Search on the Mapbox path:
$ curl -s 'http://127.0.0.1:8080/geocoding/v5/mapbox.places/Kneza%20Mihaila%2010%2C%20Beograd.json?access_token=pk.eyJ1IjoibG9jYWwiLCJhIjoibG9jYWwifQ.x&limit=1'
{"attribution":"GridPin; the data of each sheet under its own licence (gridpin sheets)","features":[{"address":"10","center":[20.4591642,44.8161152],"context":[{"id":"place.Stari Grad","text":"Stari Grad"},{"id":"country.rs","short_code":"rs","text":"Serbia"}],"geometry":{"coordinates":[20.4591642,44.8161152],"type":"Point"},"id":"address.-1899668540656762","place_name":"10 Kneza Mihaila, Stari Grad, Serbia","place_type":["address"],"properties":{"accuracy":"rooftop","match_level":"PointAddress","source":"gridpin","weak":false},"relevance":0.9399999976158142,"text":"Kneza Mihaila","type":"Feature"}],"query":["kneza","mihaila","10,","beograd"],"type":"FeatureCollection"}
3. Reverse search: the query is lon,lat.
$ curl -s 'http://127.0.0.1:8080/geocoding/v5/mapbox.places/20.4577,44.8171.json?access_token=pk.eyJ1IjoibG9jYWwiLCJhIjoibG9jYWwifQ.x&limit=1'
{"attribution":"GridPin; the data of each sheet under its own licence (gridpin sheets)","features":[{"address":"10","center":[20.4575757,44.8169419],"context":[{"id":"place.Stari Grad","text":"Stari Grad"},{"id":"country.rs","short_code":"rs","text":"Serbia"}],"geometry":{"coordinates":[20.4575757,44.8169419],"type":"Point"},"id":"address.-1899669832502320","place_name":"10 Zmaj-jovina, Stari Grad, Serbia","place_type":["address"],"properties":{"accuracy":"rooftop","match_level":"PointAddress","source":"gridpin","weak":false},"relevance":0.9296965003013611,"text":"Zmaj-jovina","type":"Feature"}],"query":[20.4577,44.8171],"type":"FeatureCollection"}
4. In @mapbox/mapbox-gl-geocoder 5.1.3, set origin to your server:
new MapboxGeocoder({ accessToken: "pk.eyJ1IjoibG9jYWwiLCJhIjoibG9jYWwifQ.x",
origin: "http://127.0.0.1:8080", mapboxgl })
What you get
place_name, center, context and place_type as Mapbox writes them; properties.accuracy says how the point was found and properties.weak marks an answer to check. relevance is GridPin's confidence, not a probability.
Why it works
The same engine answers as on /v1/geocode, laid out in the v5 shape. What Mapbox has and a country file does not is a stand-in: the id is made from the point.
Limits
- Version 5 only; v6 and the batch path are not served.
routing,worldviewandproximity=ipchange nothing.- The key is not checked: keep the server where only your users reach it.
- Mapbox's own map tiles still come from Mapbox; only the search moves.
The function: answer like Mapbox Geocoding. Related: a search box on your map, move off the Google Geocoding API.