Skip to content

Commit 0ee09a6

Browse files
authored
Geocoder plugin: support other built-in providers (#1852)
* expanding Geocoder plugin to support other built-in providers supported by leaflet-control-geocoder * Added geocode_zoom option for setting zoom level used to display the geocode result * fixed docstring and type notation for geocode_zoom * dropped geocode_ prefix in arguments * fixed import block (ruff --fix)
1 parent 4e6ebfe commit 0ee09a6

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

folium/plugins/geocoder.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from branca.element import MacroElement
24
from jinja2 import Template
35

@@ -19,6 +21,12 @@ class Geocoder(JSCSSMixin, MacroElement):
1921
Choose from 'topleft', 'topright', 'bottomleft' or 'bottomright'.
2022
add_marker: bool, default True
2123
If True, adds a marker on the found location.
24+
zoom: int, default 11, optional
25+
Set zoom level used for displaying the geocode result, note that this only has an effect when add_marker is set to False. Set this to None to preserve the current map zoom level.
26+
provider: str, default 'nominatim'
27+
Defaults to "nominatim", see https://github.com/perliedman/leaflet-control-geocoder/tree/2.4.0/src/geocoders for other built-in providers.
28+
provider_options: dict, default {}
29+
For use with specific providers that may require api keys or other parameters.
2230
2331
For all options see https://github.com/perliedman/leaflet-control-geocoder
2432
@@ -27,10 +35,22 @@ class Geocoder(JSCSSMixin, MacroElement):
2735
_template = Template(
2836
"""
2937
{% macro script(this, kwargs) %}
38+
39+
var geocoderOpts_{{ this.get_name() }} = {{ this.options|tojson }};
40+
41+
// note: geocoder name should start with lowercase
42+
var geocoderName_{{ this.get_name() }} = geocoderOpts_{{ this.get_name() }}["provider"];
43+
44+
var customGeocoder_{{ this.get_name() }} = L.Control.Geocoder[ geocoderName_{{ this.get_name() }} ](
45+
geocoderOpts_{{ this.get_name() }}['providerOptions']
46+
);
47+
geocoderOpts_{{ this.get_name() }}["geocoder"] = customGeocoder_{{ this.get_name() }};
48+
3049
L.Control.geocoder(
31-
{{ this.options|tojson }}
50+
geocoderOpts_{{ this.get_name() }}
3251
).on('markgeocode', function(e) {
33-
{{ this._parent.get_name() }}.setView(e.geocode.center, 11);
52+
var zoom = geocoderOpts_{{ this.get_name() }}['zoom'] || {{ this._parent.get_name() }}.getZoom();
53+
{{ this._parent.get_name() }}.setView(e.geocode.center, zoom);
3454
}).addTo({{ this._parent.get_name() }});
3555
3656
{% endmacro %}
@@ -50,12 +70,24 @@ class Geocoder(JSCSSMixin, MacroElement):
5070
)
5171
]
5272

53-
def __init__(self, collapsed=False, position="topright", add_marker=True, **kwargs):
73+
def __init__(
74+
self,
75+
collapsed: bool = False,
76+
position: str = "topright",
77+
add_marker: bool = True,
78+
zoom: Optional[int] = 11,
79+
provider: str = "nominatim",
80+
provider_options: dict = {},
81+
**kwargs
82+
):
5483
super().__init__()
5584
self._name = "Geocoder"
5685
self.options = parse_options(
5786
collapsed=collapsed,
5887
position=position,
59-
defaultMarkGeocode=add_marker,
88+
default_mark_geocode=add_marker,
89+
zoom=zoom,
90+
provider=provider,
91+
provider_options=provider_options,
6092
**kwargs
6193
)

0 commit comments

Comments
 (0)