Skip to content

Commit

Permalink
Geocoder plugin: support other built-in providers (python-visualizati…
Browse files Browse the repository at this point in the history
…on#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)
  • Loading branch information
prusswan authored and hansthen committed Jan 24, 2024
1 parent 29d041b commit 5907d68
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions folium/plugins/geocoder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from branca.element import MacroElement
from jinja2 import Template

Expand All @@ -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
Expand All @@ -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 %}
Expand All @@ -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
)

0 comments on commit 5907d68

Please sign in to comment.