The following snippet works perfectly on Chrome displaying a search bar to autofill address results. Unfortunately Safari (17.5 in my case) does not recognize .on method on mapboxGeocoder object. Is there a solution ? (Working in a Rails 7 app using Stimulus controller)
initMapboxAutofill() {
mapboxgl.accessToken = window.mapboxAccessToken;
this.mapboxGeocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl,
marker: false,
autocomplete: true,
placeholder: 'Search for an address',
});
if (this.hasGeocoderAutocompleteTarget) {
this.geocoderAutocompleteTarget.appendChild(this.mapboxGeocoder.onAdd());
if (typeof this.mapboxGeocoder.on === 'function') {
this.mapboxGeocoder.on('result', (event) => {
const { geometry } = event.result;
this.populateAddressFields(event);
const coordinates = geometry.coordinates;
this.displayStaticMap(coordinates);
});
} else {
console.error('Mapbox Geocoder "on" method is not available.'); //Safari flow
}
} else {
this.initStaticMapForExistingHome();
}
}
<script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v5.0.0/mapbox-gl-geocoder.min.js"></script>
<script id="search-js" defer src="https://api.mapbox.com/search-js/v1.0.0-beta.21/web.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/v3.5.1/mapbox-gl.js"></script>