diff --git a/folium/plugins/geocoder.py b/folium/plugins/geocoder.py index 418dcb9d01..3d267a05d0 100644 --- a/folium/plugins/geocoder.py +++ b/folium/plugins/geocoder.py @@ -1,3 +1,5 @@ +from typing import Optional + from branca.element import MacroElement from jinja2 import Template @@ -19,6 +21,12 @@ class Geocoder(JSCSSMixin, MacroElement): Choose from 'topleft', 'topright', 'bottomleft' or 'bottomright'. add_marker: bool, default True If True, adds a marker on the found location. + zoom: int, default 11, optional + 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. + provider: str, default 'nominatim' + Defaults to "nominatim", see https://github.com/perliedman/leaflet-control-geocoder/tree/2.4.0/src/geocoders for other built-in providers. + provider_options: dict, default {} + For use with specific providers that may require api keys or other parameters. For all options see https://github.com/perliedman/leaflet-control-geocoder @@ -27,10 +35,22 @@ class Geocoder(JSCSSMixin, MacroElement): _template = Template( """ {% macro script(this, kwargs) %} + + var geocoderOpts_{{ this.get_name() }} = {{ this.options|tojson }}; + + // note: geocoder name should start with lowercase + var geocoderName_{{ this.get_name() }} = geocoderOpts_{{ this.get_name() }}["provider"]; + + var customGeocoder_{{ this.get_name() }} = L.Control.Geocoder[ geocoderName_{{ this.get_name() }} ]( + geocoderOpts_{{ this.get_name() }}['providerOptions'] + ); + geocoderOpts_{{ this.get_name() }}["geocoder"] = customGeocoder_{{ this.get_name() }}; + L.Control.geocoder( - {{ this.options|tojson }} + geocoderOpts_{{ this.get_name() }} ).on('markgeocode', function(e) { - {{ this._parent.get_name() }}.setView(e.geocode.center, 11); + var zoom = geocoderOpts_{{ this.get_name() }}['zoom'] || {{ this._parent.get_name() }}.getZoom(); + {{ this._parent.get_name() }}.setView(e.geocode.center, zoom); }).addTo({{ this._parent.get_name() }}); {% endmacro %} @@ -50,12 +70,24 @@ class Geocoder(JSCSSMixin, MacroElement): ) ] - def __init__(self, collapsed=False, position="topright", add_marker=True, **kwargs): + def __init__( + self, + collapsed: bool = False, + position: str = "topright", + add_marker: bool = True, + zoom: Optional[int] = 11, + provider: str = "nominatim", + provider_options: dict = {}, + **kwargs + ): super().__init__() self._name = "Geocoder" self.options = parse_options( collapsed=collapsed, position=position, - defaultMarkGeocode=add_marker, + default_mark_geocode=add_marker, + zoom=zoom, + provider=provider, + provider_options=provider_options, **kwargs )