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

MapboxGeocoder query causing selection of first result if not matching current geocoder input.

Open CodeZeno opened this issue 5 years ago • 1 comments

I have a use case where I would like to show the Mapbox results when you focus the geocoder input. To do this I set the placeholder text to the place_name of the result and check if the current geocoder input is equal to the placeholder. If it is I perform a query on the text to get the results dropdown to show. This works great majority of the time but I have noticed if the 1st result is not the one in the current geocoder input if automatically selects it and flys to that location. Can you explain why this is triggered?

I have setup a simple fiddle here: https://jsfiddle.net/qzarfdv2/

  1. Enter your mapbox token
  2. Enter 4870 in the geocoder input and select the first option
  3. Click the map to lose focus
  4. Click the geocoder input

Because some street result comes up as the first result something triggers it to go to that result automatically.

If you enter just about any other location and do the same steps you'll see that it just displays the dropdown suggestions and does not automatically select anything.

CodeZeno avatar Jun 09 '20 03:06 CodeZeno

You've identified why this is happening, as you said, because the place_name from 4870 is 4870, Cairns, Queensland, Australia and when you search for 4870, Cairns, Queensland, Australia you get a different result.

geocoder.query means set and query that input https://github.com/mapbox/mapbox-gl-geocoder/blob/master/API.md#query so in your code it's setting and querying 4870, Cairns, Queensland, Australia which has a different result.

So in this case it's working as expected, now i get that's not working as you'd like, but it's still doing exactly what it's designed to do.

This is quite similar to what Jayen was asking for at #326

If you have a concrete proposal of what could be changed, or a PR I'd be happy to take a look.

andrewharvey avatar Jun 17 '20 06:06 andrewharvey