smeijer / leaflet-geosearch

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

Using LocationIQProvider in leaflet-geosearch library #367

Closed adrianwilk89 closed 7 months ago

adrianwilk89 commented 1 year ago

"no result" return 404 from locationIQ which causes leaflet-geosearch to crash and "notFoundMessage" doesn't show up, is any possibility to avoid this behavior ?

my code looks like this

const searchControl = new GeoSearchControl({ provider: new LocationIQProvider({ params: { key: process.env.LOCATION_IQ_TOKEN, }, }), style: 'bar', notFoundMessage: 'location not found', });

Console output bellow:

geosearch.module.js:1 GET https://locationiq.org/v1/search.php?key=pk.425ef3dxxxxxxd0b04ef71&q=2343243423432&format=json 404 r.search @ geosearch.module.js:1 eval @ geosearch.module.js:1 autoSearch @ geosearch.module.js:1 n @ geosearch.module.js:1 eval @ geosearch.module.js:1 setTimeout (asynchronicznie) eval @ geosearch.module.js:1 geosearch.module.js:1 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '0') at eval (geosearch.module.js:1:22355) at Array.map () at n.parse (geosearch.module.js:1:22286) at eval (geosearch.module.js:1:11482)

t1m0thy commented 7 months ago

I had the same issues, and I have a PR #387 that fixes it. In the meantime, I am using this custom provider that checks for response.data.error and returns an empty list when that has a value. this has fixed it for me.


export default class LocationIQProvider extends OpenStreetMapProvider {
  constructor(options) {
    super({
      ...options,
      searchUrl: `https://locationiq.org/v1/search.php`,
      reverseUrl: `https://locationiq.org/v1/reverse.php`,
    });
  }

  parse(response) {
    if (response.data.error) {
      return [];
    }
    return super.parse(response);
  }
}
smeijer commented 7 months ago

Fixed by #387