smeijer / leaflet-geosearch

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

Multiple Providers #153

Open brotkiste opened 6 years ago

brotkiste commented 6 years ago

is it possible to use multiple providers with the leaflet controls? For example using nominatim for points in osm and another provider for features in a geojson layer?

smeijer commented 6 years ago

I guess you could create your own provider that wrap's the two you want to use. This custom provider can then be assigned to the control.

brotkiste commented 6 years ago

ok, thanks :) Would it make sense to do a pull request on this provider that wraps a list of other providers? I don't know when I will have time to do this

bradstdev commented 5 years ago

@brotkiste did you figure out how to wrap multiple providers (needing OSM + my own geoJson layers searched).

Any clues would be very much appreciated!

brotkiste commented 5 years ago

@austere-rm Hi, sorry unfortunately I didn't have the time for that so far :/ My use case would be the same.

marct83 commented 1 year ago

Did anyone figure this out? Looking into doing this. Unsure how to add a provider and trigger search inside the custom provider.

I'm trying to wrap the google provider.

marct83 commented 1 year ago

I got this working..


export class Provider {
  public landLocationRegEx: RegExp =
    /[NSEW]{2}\s\d{1,2}-\d{1,2}-\d{1,2}-[NSEW]\d/;
  public googleProvider = new GoogleProvider({
    params: {
      key: environment.google.mapsKey,
    },
  });
  constructor(private service: TestService) {}

  parse(
    response: ParseArgument<TestType.LandLocationLookupResult>,
    query
  ) {
    return [
      {
        x: Number(response.data.lng), // lon
        y: Number(response.data.lat), // lat
        label: query, // formatted address
        bounds: null,
        raw: response,
      },
    ];
  }

  async search({ query }) {
    if (this.landLocationRegEx.test(query)) {
      const test = await this.service.LandLocationLookup(query);
      return this.parse({ data: test }, query);
    } else {
      return this.googleProvider.search({ query });
    }
  }
}