Address search on a folium map

folium's Geocoder plugin puts a search box on a notebook map, and by default it sends every search to the public Nominatim, which allows no autocomplete and one request a second. Point it at your own server and it suggests as you type.

New Geocode addresses Python Research

What you have

folium 1.0.0 (checked) in your Python environment, a server you can run, and the country file. The script that writes the map:

make_map.pyfile
import folium
from folium.plugins import Geocoder

m = folium.Map(location=[44.8171, 20.4577], zoom_start=15)
Geocoder(provider="photon", provider_options={
    "serviceUrl": "http://127.0.0.1:8080/photon/api/",
    # the house number first: the Photon provider leaves it out of a label by default
    "nameProperties": ["housenumber", "street", "name", "postcode", "city", "country"],
}).add_to(m)
m.save("map.html")

Steps

1. Start the server:

terminalCLI
$ gridpin serve serbia.bin --port 8080

2. Write the map; the search box asks your server:

terminalCLI
$ python make_map.py && grep -o '"serviceUrl": "[^"]*"' map.html
"serviceUrl": "http://127.0.0.1:8080/photon/api/"

3. What the box receives as you type:

terminalCLI
$ curl -s 'http://127.0.0.1:8080/photon/api/?q=Kneza+Mihaila+10&lat=44.8171&lon=20.4577&zoom=15&limit=2'
{"features":[{"geometry":{"coordinates":[20.4591642,44.8161152],"type":"Point"},"properties":{"city":"Stari Grad","country":"Serbia","countrycode":"RS","extra":{"match_level":"PointAddress","match_status":"M"},"housenumber":"10","osm_key":"place","osm_value":"house","street":"Kneza Mihaila","type":"house"},"type":"Feature"},{"geometry":{"coordinates":[20.5486023,44.7694874],"type":"Point"},"properties":{"city":"Kaluđerica","country":"Serbia","countrycode":"RS","extra":{"match_level":"PointAddress","match_status":"M"},"housenumber":"10","osm_key":"place","osm_value":"house","street":"Kneza Mihaila","type":"house"},"type":"Feature"}],"type":"FeatureCollection"}

What you get

A search box that lists the houses with their numbers as you type and puts a marker on the chosen one (checked in a browser with the Monaco file, which gave the label “27, Boulevard des Moulins, 98000, Monaco, Monaco”; the Serbia server above answers the same way). Without nameProperties the label loses the house number: the Photon provider of leaflet-control-geocoder leaves it out by default.

Why it works

folium passes the provider by name to leaflet-control-geocoder; its Photon provider takes any serviceUrl, and GridPin's server answers on /photon/api in Photon's shape.

Limits

  • folium loads leaflet-control-geocoder and the map tiles from the internet: the map needs a network, the searches go only to your server.
  • A weak answer is not marked in the list: the Photon provider does not read GridPin's extra.match_status.
  • The server must be reachable from the browser that opens the map.

The function: answer like Photon. Related: a search box on your map, offline geocoding in Python.