For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /index.md.

Introduction

leaflet-geosearch adds support for geocoding (address lookup, a.k.a. geosearching) to your (web) application. It comes with controls to be embedded in your Leaflet map.

It comes with providers for OpenStreetMap, Google, Bing, Esri, LocationIQ, OpenCage and more, see the Providers section. You can also write your own.

Although this project is named leaflet-geosearch, the providers also work without Leaflet, for example to look up addresses in a form. Import them from leaflet-geosearch/providers when Leaflet isn't available, like during server-side rendering (see Usage).

Installation

npm
yarn
pnpm
bun
deno
npm install leaflet-geosearch

Include the CSS file somewhere. Where you do this depends a bit on your build pipeline.

import 'node_modules/leaflet-geosearch/dist/geosearch.css';

Using a CDN

To load leaflet-geosearch from a CDN instead of using a bundler, use jsDelivr or unpkg. Both serve the files from the npm package:

FileUse
dist/geosearch.cssThe styles of the control.
dist/bundle.min.jsFor a <script> tag. Sets window.GeoSearch. dist/geosearch.umd.js is the same file.
dist/geosearch.module.jsAn ES module for <script type="module">. It imports leaflet, see ES modules.

Pin the major version, like @5 below, instead of @latest. A new major version can have breaking changes, and with @latest your page would get it without notice.

Include the CSS file in the head section of your page, and the JavaScript file before the closing body tag. When you use it with Leaflet, include it after Leaflet's JavaScript file:

jsDelivr
unpkg
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet-geosearch@5/dist/geosearch.css" />
<script src="https://cdn.jsdelivr.net/npm/leaflet-geosearch@5/dist/bundle.min.js"></script>

For Leaflet 2, load its leaflet-global.js first, see Leaflet 2.

Now you're ready to use the control or providers. Just remember that your "import" syntax will be a bit different from what the docs are using.

// when the docs use an import:
import { OpenStreetMapProvider } from 'leaflet-geosearch';
const provider = new OpenStreetMapProvider();

// you want to get it of the window global
const provider = new GeoSearch.OpenStreetMapProvider();

ES modules

dist/geosearch.module.js imports leaflet by name, which a browser can't resolve on its own. Add an import map that points leaflet to the same Leaflet your map uses. This works well with Leaflet 2, which is published as ES modules:

<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.css"
/>
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/leaflet-geosearch@5/dist/geosearch.css"
/>

<script type="importmap">
  {
    "imports": {
      "leaflet": "https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.js",
      "leaflet-geosearch": "https://cdn.jsdelivr.net/npm/leaflet-geosearch@5/dist/geosearch.module.js"
    }
  }
</script>

<script type="module">
  import { Map, TileLayer } from 'leaflet';
  import { GeoSearchControl, OpenStreetMapProvider } from 'leaflet-geosearch';

  const map = new Map('map').setView([52.09, 5.12], 8);
  new TileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);

  map.addControl(
    new GeoSearchControl({ provider: new OpenStreetMapProvider() }),
  );
</script>

Services that rewrite the imports for you, like jsDelivr's /+esm or esm.sh, load their own copy of Leaflet (leaflet@latest, which is 1.9). If your map uses a different Leaflet, the page ends up with two copies of Leaflet, and the control may not work with your map. The import map avoids that. With esm.sh, you can add ?external=leaflet so it uses the Leaflet from your import map:

"leaflet-geosearch": "https://esm.sh/leaflet-geosearch@5?external=leaflet"

Example

A basic working example would look like:

// Either get GeoSearch from the window global, or import from `leaflet-geosearch`
// import * as GeoSearch from 'leaflet-geosearch';

const map = L.map('map').setView([51.505, -0.09], 13);

L.tileLayer('//tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);

const search = new GeoSearch.GeoSearchControl({
  provider: new GeoSearch.OpenStreetMapProvider(),
});

map.addControl(search);

Providers

leaflet-geosearch uses so-called "providers" to take care of building the correct service URL and parsing the retrieved data into a uniform format. Thanks to this architecture, it is trivial to add your own providers, so you can use your own geocoding service.

When OpenStreetMap does not match your needs; you can also choose to use the Bing, Esri, Google, LocationIQ, or OpenCage providers. Most of those providers do however require API keys. See the documentation pages on the relevant organisations on how to obtain these keys.

In case you decide to write your own provider, please consider submitting a PR to share your work with us.

Providers are unaware of any options you can give them. They are simple proxies to their endpoints. There is only one special property, and that is the params option. The difference being; that params will be included in the endpoint url. Being Often used for API KEYS, while the other attributes can be used for provider configuration.

Browser support / Polyfills

This project is written with the latest technologies in mind. Thereby it is required to include some polyfills when you wish to support older browsers. These polyfills are recommended for IE and Safari support: