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 /upgrading/v3.md.

Upgrading from 2.x to 3.0

3.0 is a rewrite in TypeScript with a new build. The control and the built-in providers are used the same way: new GeoSearchControl({ provider }), the same options and the same events. What changed are the file names in the package, custom providers, and a limit on the suggestions. If you go further to 5.x, read 3.x to 4.0 and 4.x to 5.0 next.

Dependencies and requirements

Package or requirement2.x3.0What to do
leafletnot a dependency, the global L is used^1.6.0 (optional)use Leaflet 1.6 or newer
lodash.debounce, nodent-runtimedependenciesnone, bundlednothing
TypeScript typesnot includedincludeddrop your own declarations, if any

Updating the package

package.json
{
  "dependencies": {
    "leaflet-geosearch": "^2.7.0"
    "leaflet-geosearch": "^3.0.0"
  }
}

The imports stay the same:

import { GeoSearchControl, OpenStreetMapProvider } from 'leaflet-geosearch';

Files in the package

File2.x3.0
CommonJSlib/index.jsdist/geosearch.js
ES modulenonedist/geosearch.module.js
Browser build (<script>)dist/bundle.min.jsdist/geosearch.umd.js
Stylesheetdist/style.cssdist/geosearch.css
Stylesheet (source)assets/css/leaflet.cssassets/css/leaflet.css

If you import the stylesheet or load the browser build by path, change the path:

import 'leaflet-geosearch/dist/style.css'; 
import 'leaflet-geosearch/dist/geosearch.css'; 
<script src="https://unpkg.com/leaflet-geosearch@2/dist/bundle.min.js"></script> 
<script src="https://unpkg.com/leaflet-geosearch@3/dist/geosearch.umd.js"></script> 

The browser build still puts everything on window.GeoSearch, and Leaflet has to be loaded before it. 3.x has no dist/bundle.min.js. 5.x has it again, as a copy of the browser build.

Suggestions are limited to 5

3.0 shows at most 5 suggestions, 2.x showed every result of the provider. maxSuggestions sets the number:

2.x → 3.0
new GeoSearchControl({ provider });
// every result of the provider is suggested
// at most 5 results are suggested

new GeoSearchControl({ provider, maxSuggestions: 10 });

Requests use https

2.x used the protocol of the page for provider requests, so a page on http: asked the provider over http:. 3.0 always uses https:. Nothing to do, unless you run a provider of your own that only answers over http:.

Custom providers

The base class of the providers is exported as JsonProvider, Provider is gone. endpoint gets { query, type } instead of { query, protocol }, and returns the full https: url:

import { Provider } from 'leaflet-geosearch'; 
import { JsonProvider } from 'leaflet-geosearch'; 

class MyProvider extends Provider { 
class MyProvider extends JsonProvider { 
  endpoint({ query, protocol }) { 
    return `${protocol}//example.com/search?q=${query}`; 
  endpoint({ query }) { 
    return this.getUrl('https://example.com/search', { q: query }); 
  }

  parse({ data }) {
    return data.map((r) => ({ x: r.lon, y: r.lat, label: r.name, bounds: null, raw: r }));
  }
}

parse({ data }) and the shape of a result (x, y, label, bounds, raw) are the same. See Custom providers for the current version.

The OpenStreetMap provider of 2.x looked up a picked suggestion again with endpointReverse. That method is gone in 3.0. Since 5.0, provider.reverse() does reverse geocoding: Reverse geocoding.

New in 3.0

  • the HERE, OpenCage and Algolia providers (Algolia was removed again in 4.0), and LocationIQProvider in the main export (2.x had the file, but didn't export it)
  • maxSuggestions
  • TypeScript types