leaflet-plugin icon indicating copy to clipboard operation
leaflet-plugin copied to clipboard

Allow using existing input control

Open stevage opened this issue 8 years ago • 1 comments

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.

stevage avatar Aug 24 '17 07:08 stevage

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.

stevage avatar Aug 30 '17 00:08 stevage