Upgrading from 4.x to 5.0
5.0 fixes a lot of long-standing issues, and some of those fixes change what you see. The API is the same: nothing was removed or renamed, and the browser support didn't change. Each section shows what changed from 4.x (red) to 5.0 (green), and how to get the old behaviour back if you relied on it.
Dependencies and requirements
Updating the package
@googlemaps/js-api-loader 2
GoogleProvider loads the Maps JavaScript API with @googlemaps/js-api-loader. It's an optional dependency, so npm installs it with leaflet-geosearch. If your own package.json lists the 1.x loader too, update it, otherwise npm keeps a second copy for you:
The provider options don't change, see Google for the few that are gone.
@types/google.maps
The type declarations of GoogleProvider refer to the google.maps namespace. In 4.x, TypeScript projects without @types/google.maps failed to compile (#349). 5.0 installs it with the package. If you only added it for leaflet-geosearch, you can remove it:
TypeScript
The types are checked with TypeScript 4.7 to 5.9, with moduleResolution set to node, bundler and node16. new GeoSearchControl(...) type-checks now, in 4.x only GeoSearchControl(...) did (#334).
Labels are escaped
The default resultFormat and popupFormat escape the label, since it comes from the provider (OpenStreetMap names are user edited).
If your provider returns html in its labels on purpose, render it with your own formatters:
A picked suggestion isn't searched again
Picking a suggestion (click, or arrows and enter) shows it right away. Suggestions without coordinates are still searched.
To always search again, like before:
autoClose keeps the marker
With autoClose and without keepResult, the marker of a picked result was removed right after it was added, so the map moved to a place without a marker. The marker and its popup stay now. The text in the box is still cleared without keepResult, and closing the box by hand or pressing escape still removes the marker.
For no marker, like before:
Failed searches fire an event
A failed search (offline, a server error, a blocked request) fires geosearch/error on the map, and the control shows its error style until the user types again.
Fewer autocomplete requests
Autocomplete no longer searches again for the same text (keys like shift or tab), drops answers that come back after a newer search started, and also searches after a paste or on mobile keyboards. Nothing to change in your code. Custom providers get an optional signal to cancel outdated requests, ignoring it is fine:
Options
Options passed as undefined keep their default, and marker options are merged with the defaults. Wrapper components often pass every prop, set or not.
Providers
GoogleProvider uses @googlemaps/js-api-loader 2. The option names stay the same (apiKey, version, language, ...). If the page already loaded the Maps JavaScript API, the provider uses it instead of loading it again. url, nonce, retries and id don't exist in the new loader, they are ignored with a warning in the console:
Esri
One, like before:
OpenStreetMap, LocationIQ and CivilDefenseMap
Package
- the npm package no longer ships the tests, fixtures and the old docs theme.
package.jsonhas"sideEffects": ["*.css"], so bundlers drop what you don't use. Importing the css still works.@types/google.mapsis installed with the package, TypeScript projects don't need to add it.exportslists thetypescondition first. WithmoduleResolution: "bundler"or"node16", TypeScript found the types only through a fallback before.- the bundles keep their names and paths:
dist/geosearch.js,geosearch.module.js,geosearch.umd.js,bundle.min.jsandgeosearch.css. Direct links likeunpkg.com/leaflet-geosearch/dist/geosearch.umd.jskeep working. - new entry
leaflet-geosearch/providerswith the providers only, without Leaflet. Importing the main entry loads Leaflet, which fails on the server ("window is not defined"). On the server the providers need a globalfetch, which Node.js has since version 18:
New in 5.0
These are new options, nothing changes unless you use them:
- multiple providers in one control, with a provider select and a fallback: Multiple providers
zoomToBounds,fitBoundsOptionsandflyTo: Zoom on a resultshowResultListOnSubmit,minQueryLengthandcacheSize: Options- reverse geocoding with
provider.reverse(), andreverseOnDragEndfor a dragged marker: Reverse geocoding, Address of a dragged marker reset(),setQuery()andsetLabels(): MethodsresultListPosition,theme,searchIcon,markerPopupandpopupOptions: Leaflet control- the
geosearch/beforeshowlocationevent: Events - the result list follows the ARIA combobox pattern for screen readers: Accessibility
- the Photon provider: Photon