smeijer / leaflet-geosearch

A geocoding/address-lookup library supporting various api providers.
https://smeijer.github.io/leaflet-geosearch/
MIT License
1.03k stars 273 forks source link

AlgoliaProvider not working if language option is used #234

Open MaRaSu opened 4 years ago

MaRaSu commented 4 years ago

If "language"-option is used with AlgoliaProvider the REST API response from Algolia changes as described in their documentation (no default properties), which AlgoliaProvider does not handle properly but throws exception in getLabel-function.

Would be good to mention at least in documentation.

yznts commented 3 years ago

Label generation is a bit tricky, that functionality is not provided by Algolia API. Seems like response structure was changed after providing an option. Please, can you provide a bit more details? Error message, your provider setup, etc.

MaRaSu commented 3 years ago

@yuriizinets, sorry for slow reply, somehow did not notice your comment.

My provider setup is:

            const searchButton = new GeoSearch.GeoSearchControl({
                provider: new GeoSearch.AlgoliaProvider({
                    params: {
                        countries: 'fi',
                        language: 'fi'
                    }
                }),
                style: 'button',
                position: 'headertopleft',
                showMarker: false,
                searchLabel: i18next.t('geocodingButtonLabel'),
                retainZoomLevel: true,
                zoomLevel: 13,
                classNames: {
                    container: 'leaflet-bar leaflet-control leaflet-control-geosearch search-button-observer',
                    input: 'input'
                },
                autoClose: true
            });

And with that I get error code (from Chrome JS console) when matching of partially written address happens:

geosearch.module.js:1 Uncaught (in promise) TypeError: Cannot read property 'find' of undefined
    at r.o.findBestMatchLevelIndex (geosearch.module.js:1)
    at r.o.getLabel (geosearch.module.js:1)
    at geosearch.module.js:1
    at Array.map (<anonymous>)
    at r.o.parse (geosearch.module.js:1)
    at geosearch.module.js:1

If I remove AlgoliaProvider param "language" then it works. As I stated above the reason seems to be that with language-option the format for Algolia REST API response structure changes slightly to have no default properties which leafleat-geosearch module is relying on...