Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autoCollapse = true option makes an empty text of a search:locationfound event #277

Open
easz opened this issue Mar 19, 2021 · 2 comments
Open

Comments

@easz
Copy link

easz commented Mar 19, 2021

Basically I want to show search input text in the popup of a search marker.

new L.Control.Search({
  url: 'https://nominatim.openstreetmap.org/search?format=json&q={s}',
  jsonpParam: 'json_callback',
  propertyName: 'display_name',
  propertyLoc: ['lat', 'lon'],
  marker: search_marker,
  autoCollapse: false, // otherwise the search input text will not be availble to use
}).on("search:locationfound", function(e) {
  search_marker.getPopup().setContent(e.text);
});

The problem is that I have to set autoCollapse to false, otherwise the text attribute of the event `search:locationfoundz will be empty.

Is it intended? bug? or should I do differently?

Demo: (try to set autoCollapse to true to see the problem)
https://jsfiddle.net/ewr2y67b/6/

@jt196
Copy link

jt196 commented Aug 19, 2021

Can verify this, been looking for ways to save the address of the searched item and up against the same problem - even grabbing the innerHTML of the JS DOM class "search-tip" doesn't work. Perhaps this is resolved in v3.0?

@PepeQuiroga
Copy link

This is the solution. When the event locationfound is fired on _handleSubmit, this occurs:

this.showLocation(loc, this._input.value);
this.fire('search:locationfound', {
latlng: loc,
text: this._input.value,
layer: loc.layer ? loc.layer : null
});

but, when autoCollapse == true, then showLocation sets this._input.value = ''. The solution is to
obtain a copy of this._input.value before showLocation:

var sText = this._input.value;
this.showLocation(loc, this._input.value);
this.fire('search:locationfound', {
latlng: loc,
text: sText,
layer: loc.layer ? loc.layer : null
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants