mapbox-gl-geocoder icon indicating copy to clipboard operation
mapbox-gl-geocoder copied to clipboard

Need to capture event for geocode when the result is not selected

Open girishts opened this issue 6 years ago • 3 comments

Hi ,

I need the details of of how to capture event when we type something in geocoder and doesn't select any results from the autocomplete ?

I referred the below link and added the marker after selecting the result using geocoder.on('result', function(ev) {} ). This will get the control only if we select results from autocomplete. How to capture the event where user types something in the geocoder and doesn't select any results form autocomplete. I need this to clear previously selected address from my component state.

https://docs.mapbox.com/mapbox-gl-js/example/point-from-geocoder-result/

Thanks Girish T S

girishts avatar Apr 04 '19 09:04 girishts

Something like geocoder._inputEl.addEventListener('blur', function (e) { console.log('blur', e) });?

andrewharvey avatar Apr 04 '19 11:04 andrewharvey

map.addControl( new MapboxGeocoder({ accessToken: mapboxgl.accessToken, mapboxgl: mapboxgl, getItemValue: e => { console.log(e); } }) );

e contains everything you could want to know about the selected location

BenAhlander avatar Jan 17 '20 19:01 BenAhlander

Here is a working example map.addControl( new MapboxGeocoder({ accessToken: mapboxgl.accessToken, mapboxgl: mapboxgl, getItemValue: ({ center, place_name }) => { Api.logUserGeoSearch(center[1], center[0], place_name); return place_name; } }) );

BenAhlander avatar Jan 17 '20 19:01 BenAhlander