Skip to main content

Supplement forward geocoding search results with another data source

This example adds a geocoding control on top of a web map and supplements the place data available through the Mapbox Geocoding API with custom data from a local data source.

Custom data is loaded into the example using the customData GeoJSON object. The forwardGeocoder function takes a user's query string and performs local geocoding to supplement Mapbox Geocoding API results with results from customData, adding a tree emoji (🌲) as a prefix for custom data results. Finally, it uses .addControl to add the mapbox-gl-geocoder plugin to the map, with the localGeocoder option defined as the forwardGeocoder function.

Search for Lincoln Park to see a custom feature appear as a search result.

<!DOCTYPE html>
<meta charset="utf-8">
<title>Supplement forward geocoding search results with another data source</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="" rel="stylesheet">
<script src=""></script>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
<script src=""></script>
<link rel="stylesheet" href="" type="text/css">

<div id="map"></div>

mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
const map = new mapboxgl.Map({
container: 'map',
// Choose from Mapbox's core styles, or make your own style with Mapbox Studio
style: 'mapbox://styles/mapbox/dark-v11',
center: [-87.6244, 41.8756],
zoom: 13

// Load custom data to supplement the search results.
const customData = {
'features': [
'type': 'Feature',
'properties': {
'title': 'Lincoln Park is special'
'geometry': {
'coordinates': [-87.637596, 41.940403],
'type': 'Point'
'type': 'Feature',
'properties': {
'title': 'Burnham Park is special'
'geometry': {
'coordinates': [-87.603735, 41.829985],
'type': 'Point'
'type': 'Feature',
'properties': {
'title': 'Millennium Park is special'
'geometry': {
'coordinates': [-87.622554, 41.882534],
'type': 'Point'
'type': 'FeatureCollection'

function forwardGeocoder(query) {
const matchingFeatures = [];
for (const feature of customData.features) {
// Handle queries with different capitalization
// than the source data by calling toLowerCase().
if (
) {
// Add a tree emoji as a prefix for custom
// data results using carmen geojson format:
feature['place_name'] = `🌲 ${}`;
feature['center'] = feature.geometry.coordinates;
feature['place_type'] = ['park'];
return matchingFeatures;

// Add the control to the map.
new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
localGeocoder: forwardGeocoder,
zoom: 14,
placeholder: 'Enter search e.g. Lincoln Park',
mapboxgl: mapboxgl

Was this example helpful?