Update leaflet-control-geocoder (#296)

Switch BR.Search to ES6 class because L.Control.Geocoder is now an ES6 class and Leaflet L.Class.extend doesn't work anymore (undefined).
This commit is contained in:
Norbert Renner 2021-03-26 21:13:38 +01:00
parent cd8ac29aab
commit fbcfd8cf8e
3 changed files with 33 additions and 34 deletions

View file

@ -1,29 +1,28 @@
BR.Search = L.Control.Geocoder.extend({
options: {
geocoder: new L.Control.Geocoder.LatLng({
next: new L.Control.Geocoder.Nominatim({
serviceUrl: 'https://nominatim.openstreetmap.org/',
}),
sizeInMeters: 800,
}),
position: 'topleft',
shortcut: {
search: 70, // char code for 'f'
},
},
initialize: function (options) {
L.Control.Geocoder.prototype.initialize.call(this, options);
L.setOptions(this, {
// i18next.t will only return 'undefined' if it is called in a static context
// (e.g. when added directly to "options:" above), so we have to call it here
placeholder: i18next.t('map.geocoder-placeholder'),
});
BR.Search = class extends L.Control.Geocoder {
constructor(options) {
super(
Object.assign(
{
geocoder: new L.Control.Geocoder.LatLng({
next: new L.Control.Geocoder.Nominatim({
serviceUrl: 'https://nominatim.openstreetmap.org/',
}),
sizeInMeters: 800,
}),
position: 'topleft',
shortcut: {
search: 70, // char code for 'f'
},
placeholder: i18next.t('map.geocoder-placeholder'),
},
options
)
);
L.DomEvent.addListener(document, 'keydown', this._keydownListener, this);
},
}
markGeocode: function (result) {
markGeocode(result) {
this._map.fitBounds(result.geocode.bbox, {
maxZoom: 17,
});
@ -37,18 +36,18 @@ BR.Search = L.Control.Geocoder.extend({
}).addTo(this._map);
return this;
},
}
clear: function () {
clear() {
if (this._geocodeMarker) {
this._map.removeLayer(this._geocodeMarker);
}
},
}
_keydownListener: function (e) {
_keydownListener(e) {
if (BR.Util.keyboardShortcutsAllowed(e) && e.keyCode === this.options.shortcut.search) {
$('#map .leaflet-control-geocoder')[0].dispatchEvent(new MouseEvent('mousedown'));
e.preventDefault();
}
},
});
}
};