Bring your own geocoding srevice to mapbox-gl-js. Based on Mapbox GL Geocoder
var geocodeNominatimRequest = function(query, mapBounds, options) {
var params = { format: "json", q: query, limit: options.limit };
var urlParams = new URLSearchParams(Object.entries(params));
return fetch("http://nominatim.openstreetmap.org/search?" + urlParams).then(function(response) {
if(response.ok) {
return response.json();
} else {
return [];
}
}).then(function(json) {
return json.map(function(result) {
return {
name: result.display_name,
lat: result.lat,
lon: result.lon,
bbox: [result.boundingbox[2], result.boundingbox[0], result.boundingbox[3], result.boundingbox[1]]
};
});
});
};
mapboxglmap.addControl(new MapboxGenericGeocoder({}, geocodeNominatimRequest));
This module exports a single class called MapboxGenericGeocoder as its default export, so in browserify or webpack, you can require it like:
var MapboxGenericGeocoder = require('mapbox-gl-generic-geocoder');