Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geocoder plugin: support other built-in providers #1852

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions folium/plugins/geocoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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.
geocode_zoom: int, default 11
Conengmo marked this conversation as resolved.
Show resolved Hide resolved
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.
geocode_provider: str, default 'nominatim'
Defaults to "nominatim", see https:/perliedman/leaflet-control-geocoder/tree/2.4.0/src/geocoders for other built-in providers.
geocode_provider_options: dict, default {}
For use with specific providers that may require api keys or other parameters.
Conengmo marked this conversation as resolved.
Show resolved Hide resolved

For all options see https:/perliedman/leaflet-control-geocoder

Expand All @@ -27,10 +33,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() }}["geocodeProvider"];

var customGeocoder_{{ this.get_name() }} = L.Control.Geocoder[ geocoderName_{{ this.get_name() }} ](
geocoderOpts_{{ this.get_name() }}['geocodeProviderOptions']
);
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() }}['geocodeZoom'] || {{ this._parent.get_name() }}.getZoom();
{{ this._parent.get_name() }}.setView(e.geocode.center, zoom);
}).addTo({{ this._parent.get_name() }});

{% endmacro %}
Expand All @@ -50,12 +68,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,
geocode_zoom: int | None = 11,
Conengmo marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as the default zoom level of 11 is not suitable for close-up locations (and only works when add_marker is False)

Question: is 11 a good default? Or would None be a better default?

And another question: should we check or correct this value when add_marker is False?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

11 would preserve the current behavior so existing users would not be caught by surprise. The real gotcha would be the case when add_marker=True (when default handler in leaflet-control-geocoder will be using fit_bounds with no regard to current map zoom). What may not be obvious to normal folium users (with no knowledge of JS) is that both the default event handler for markgeocode as well as the one defined in _template would be triggered.

Not sure if there is a need to correct (beyond keeping the current default of 11). Can you explain more?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there is a need to correct (beyond keeping the current default of 11).

That's good then. It was more a question for you. We can keep it like it is now then.

geocode_provider: str = "nominatim",
geocode_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,
geocode_zoom=geocode_zoom,
geocode_provider=geocode_provider,
geocode_provider_options=geocode_provider_options,
**kwargs
)
Loading