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
Include the CSS file somewhere. Where you do this depends a bit on your build pipeline.
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:
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:
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.
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:
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:
Example
A basic working example would look like:
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:
- babel-polyfill, for
array.includessupport. - unfetch, for
fetchrequests.