Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/folium/plugins/geocoder.py
2529 views
1
from typing import Optional
2
3
from branca.element import MacroElement
4
5
from folium.elements import JSCSSMixin
6
from folium.template import Template
7
from folium.utilities import remove_empty
8
9
10
class Geocoder(JSCSSMixin, MacroElement):
11
"""A simple geocoder for Leaflet that by default uses OSM/Nominatim.
12
13
Please respect the Nominatim usage policy:
14
https://operations.osmfoundation.org/policies/nominatim/
15
16
Parameters
17
----------
18
collapsed: bool, default False
19
If True, collapses the search box unless hovered/clicked.
20
position: str, default 'topright'
21
Choose from 'topleft', 'topright', 'bottomleft' or 'bottomright'.
22
add_marker: bool, default True
23
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.
30
31
For all options see https://github.com/perliedman/leaflet-control-geocoder
32
33
"""
34
35
_template = Template("""
36
{% macro script(this, kwargs) %}
37
38
var geocoderOpts_{{ this.get_name() }} = {{ this.options|tojavascript }};
39
40
// note: geocoder name should start with lowercase
41
var geocoderName_{{ this.get_name() }} = geocoderOpts_{{ this.get_name() }}["provider"];
42
43
var customGeocoder_{{ this.get_name() }} = L.Control.Geocoder[ geocoderName_{{ this.get_name() }} ](
44
geocoderOpts_{{ this.get_name() }}['providerOptions']
45
);
46
geocoderOpts_{{ this.get_name() }}["geocoder"] = customGeocoder_{{ this.get_name() }};
47
48
L.Control.geocoder(
49
geocoderOpts_{{ this.get_name() }}
50
).on('markgeocode', function(e) {
51
var zoom = geocoderOpts_{{ this.get_name() }}['zoom'] || {{ this._parent.get_name() }}.getZoom();
52
{{ this._parent.get_name() }}.setView(e.geocode.center, zoom);
53
}).addTo({{ this._parent.get_name() }});
54
55
{% endmacro %}
56
""")
57
58
default_js = [
59
(
60
"Control.Geocoder.js",
61
"https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.js",
62
)
63
]
64
default_css = [
65
(
66
"Control.Geocoder.css",
67
"https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.css",
68
)
69
]
70
71
def __init__(
72
self,
73
collapsed: bool = False,
74
position: str = "topright",
75
add_marker: bool = True,
76
zoom: Optional[int] = 11,
77
provider: str = "nominatim",
78
provider_options: dict = {},
79
**kwargs
80
):
81
super().__init__()
82
self._name = "Geocoder"
83
self.options = remove_empty(
84
collapsed=collapsed,
85
position=position,
86
default_mark_geocode=add_marker,
87
zoom=zoom,
88
provider=provider,
89
provider_options=provider_options,
90
**kwargs
91
)
92
93