Adds a search tool to your map. Parameters ---------- layer: GeoJson, TopoJson, FeatureGroup, MarkerCluster class object. The map layer to index in the Search view. search_label: str, optional 'properties' key in layer to index Search, if layer is GeoJson/TopoJs
| 12 | |
| 13 | |
| 14 | class Search(JSCSSMixin, MacroElement): |
| 15 | """ |
| 16 | Adds a search tool to your map. |
| 17 | |
| 18 | Parameters |
| 19 | ---------- |
| 20 | layer: GeoJson, TopoJson, FeatureGroup, MarkerCluster class object. |
| 21 | The map layer to index in the Search view. |
| 22 | search_label: str, optional |
| 23 | 'properties' key in layer to index Search, if layer is GeoJson/TopoJson. |
| 24 | search_zoom: int, optional |
| 25 | Zoom level to set the map to on match. |
| 26 | By default zooms to Polygon/Line bounds and points |
| 27 | on their natural extent. |
| 28 | geom_type: str, default 'Point' |
| 29 | Feature geometry type. "Point", "Line" or "Polygon" |
| 30 | position: str, default 'topleft' |
| 31 | Change the position of the search bar, can be: |
| 32 | 'topleft', 'topright', 'bottomright' or 'bottomleft', |
| 33 | placeholder: str, default 'Search' |
| 34 | Placeholder text inside the Search box if nothing is entered. |
| 35 | collapsed: boolean, default False |
| 36 | Whether the Search box should be collapsed or not. |
| 37 | **kwargs. |
| 38 | Assorted style options to change feature styling on match. |
| 39 | Use the same way as vector layer arguments. |
| 40 | |
| 41 | See https://github.com/stefanocudini/leaflet-search for more information. |
| 42 | |
| 43 | """ |
| 44 | |
| 45 | _template = Template( |
| 46 | """ |
| 47 | {% macro script(this, kwargs) %} |
| 48 | var {{this.layer.get_name()}}searchControl = new L.Control.Search({ |
| 49 | layer: {{this.layer.get_name()}}, |
| 50 | {% if this.search_label %} |
| 51 | propertyName: '{{this.search_label}}', |
| 52 | {% endif %} |
| 53 | collapsed: {{this.collapsed|tojson|safe}}, |
| 54 | textPlaceholder: '{{this.placeholder}}', |
| 55 | position:'{{this.position}}', |
| 56 | {% if this.geom_type == 'Point' %} |
| 57 | initial: false, |
| 58 | {% if this.search_zoom %} |
| 59 | zoom: {{this.search_zoom}}, |
| 60 | {% endif %} |
| 61 | hideMarkerOnCollapse: true |
| 62 | {% else %} |
| 63 | marker: false, |
| 64 | moveToLocation: function(latlng, title, map) { |
| 65 | var zoom = {% if this.search_zoom %} {{ this.search_zoom }} {% else %} map.getBoundsZoom(latlng.layer.getBounds()) {% endif %} |
| 66 | map.flyTo(latlng, zoom); // access the zoom |
| 67 | } |
| 68 | {% endif %} |
| 69 | }); |
| 70 | {{this.layer.get_name()}}searchControl.on('search:locationfound', function(e) { |
| 71 | {{this.layer.get_name()}}.setStyle(function(feature){ |
nothing calls this directly
no test coverage detected
searching dependent graphs…