Use the geocoder without a map
Use the mapbox-gl-geocoder control to search for places using Mapbox Search API without an associated map view. See the example: https://docs.mapbox.com//mapbox-gl-js/example/mapbox-gl-geocoder-no-map/
by jscastro76
HTML
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.5.1/mapbox-gl-geocoder.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js"></script>
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css">
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.5.1/mapbox-gl-geocoder.css">
<!-- Promise polyfill script required to use Mapbox GL Geocoder in IE 11 -->
<div id="geocoder"></div>
<div class="position">
<div id="coords"></div>
</div>
CSS
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
#geocoder {
z-index: 1;
width: 100%;
text-align: center;
top: 20px;
}
.mapboxgl-ctrl-geocoder {
min-width: 100%;
}
.position {
position: absolute;
background: #fff;
min-width: 100px;
bottom: 50px;
left: 10px;
font-family: 'Open Sans', sans-serif;
}
JavaScript
mapboxgl.accessToken = 'PASTE HERE YOUR TOKEN';
var geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
types: 'country,region,place,postcode,locality,neighborhood'
});
geocoder.addTo('#geocoder');
geocoder.on('result', function(r) {
console.log(r);
document.getElementById("coords").innerHTML = r.result.center;
})