Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/folium/plugins/geocoder.py
1578 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
"""
37
{% macro script(this, kwargs) %}
38
39
var geocoderOpts_{{ this.get_name() }} = {{ this.options|tojavascript }};
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
49
L.Control.geocoder(
50
geocoderOpts_{{ this.get_name() }}
51
).on('markgeocode', function(e) {
52
var zoom = geocoderOpts_{{ this.get_name() }}['zoom'] || {{ this._parent.get_name() }}.getZoom();
53
{{ this._parent.get_name() }}.setView(e.geocode.center, zoom);
54
}).addTo({{ this._parent.get_name() }});
55
56
{% endmacro %}
57
"""
58
)
59
60
default_js = [
61
(
62
"Control.Geocoder.js",
63
"https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.js",
64
)
65
]
66
default_css = [
67
(
68
"Control.Geocoder.css",
69
"https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.css",
70
)
71
]
72
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
):
83
super().__init__()
84
self._name = "Geocoder"
85
self.options = remove_empty(
86
collapsed=collapsed,
87
position=position,
88
default_mark_geocode=add_marker,
89
zoom=zoom,
90
provider=provider,
91
provider_options=provider_options,
92
**kwargs
93
)
94
95