leaflet-plugin
leaflet-plugin copied to clipboard
Allow using existing input control
It would be really handy if we could use an existing Input control (outside the Leaflet map), rather than creating one.
I'd appreciate any feedback on the feasibility of implementing this, as we may implement it ourselves.
Ok, so this turned out to be pretty easy to implement, but not in a way which is likely to survive a PR. I changed the first few lines of onAdd to:
onAdd: function (map) {
this._body = document.body || document.getElementsByTagName('body')[0];
var container;
if (this.options.inputId) {
this._input = L.DomUtil.get(this.options.inputId);
container = this._input.parentElement;
} else {
container = L.DomUtil.create('div', 'leaflet-pelias-control leaflet-bar leaflet-control');
this._input = L.DomUtil.create('input', 'leaflet-pelias-input', container);
}
this._container = container;
// unchanged from here...
this._input.spellcheck = false;
The awkward bit is that calling geocoder.addTo(map) triggers other code in Leaflet which always moves the control inside the map. So instead, I do this:
map.geocoder._map = map;
map.geocoder.onAdd(map);
It works pretty well.