Search Control#

Example#

from ipyleaflet import Map, SearchControl, Marker, AwesomeIcon

m = Map(zoom=3, center=[19.1646, 72.8493])

marker = Marker(icon=AwesomeIcon(name="check", marker_color='green', icon_color='darkgreen'))

m.add_control(SearchControl(
  position="topleft",
  url='https://nominatim.openstreetmap.org/search?format=json&q={s}',
  zoom=5,
  marker=marker
))

m

You can add a callback that will be run when on search found:

m = Map(center=[47, 2], zoom=5)

search = SearchControl(
    position="topleft",
    url='https://cartoradon.irsn.fr/commune.py/communes/search/FR/{s}?',
    zoom=5
)
m.add_control(search)

def on_found(**kwargs):
    # Print the result of the search (text, location etc)
    print(kwargs)

search.on_location_found(on_found)

m

You can also search features from GeoJSON layers.

import json
import os
import requests

from ipyleaflet import AwesomeIcon, GeoJSON, Map, Marker, LayerGroup, SearchControl

m = Map(zoom=3, center=[19.1646, 72.8493])

if not os.path.exists('countries.geo.json'):
      url = 'https://raw.githubusercontent.com/jupyter-widgets/ipyleaflet/master/examples/countries.geo.json'
      r = requests.get(url)
      with open('countries.geo.json', 'w') as f:
        f.write(r.content.decode("utf-8"))

with open("countries.geo.json") as f:
    data = json.load(f)

countries = GeoJSON(data=data)

layer_group = LayerGroup(layers=(countries,))
marker = Marker(icon=AwesomeIcon(name="check", marker_color='green', icon_color='darkred'))

m.add_control(SearchControl(
  position="topleft",
  layer=layer_group,
  zoom=4,
  property_name='name',
  marker=marker
))

m

Attributes and methods#

class ipyleaflet.leaflet.SearchControl(**kwargs)[source]#

SearchControl class, with Control as parent class.

url#

The url used for the search queries.

Type

string, default “”

layer#

The LayerGroup used for search queries.

Type

default None

zoom#

The zoom level after moving to searched location, by default zoom level will not change.

Type

int, default None

marker#

The marker used by the control.

Type

default Marker()

found_style#

Style for searched feature when searching in LayerGroup.

Type

default {‘fillColor’: ‘#3f0’, ‘color’: ‘#0f0’}

on_feature_found(callback, remove=False)[source]#

Add a found feature event listener for searching in GeoJSON layer.

Parameters
  • callback (callable) – Callback function that will be called on found event when searching in GeoJSON layer.

  • remove (boolean) – Whether to remove this callback or not. Defaults to False.

on_location_found(callback, remove=False)[source]#

Add a found location event listener. The callback will be called when a search result has been found.

Parameters
  • callback (callable) – Callback function that will be called on location found event.

  • remove (boolean) – Whether to remove this callback or not. Defaults to False.