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

Define own Nominatim URL #213

Closed Cirrusware closed 4 years ago

Cirrusware commented 4 years ago

Hi,

I would like to define my nominatim url, but I would have to work on the provider.js file.

Is it possible ?

Thank

smeijer commented 4 years ago

I'm working on the next version in #215. Starting from v3, this will be quite trivial. In fact, that's exactly what the LocationIQ provider does. LocationIQ is another nominatim provider under a different URL.

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

class MyProvider extends OpenStreetMapProvider {
  constructor(options) {
    super({
      ...options,
      searchUrl: 'https://example.com/api/search',
      reverseUrl: 'https://example.com/api/reverse',
    });
  }
}

map.addControl(new GeoSearchControl({
  provider,
}));

In the current version it should be possible by overriding the endpoint methods. It's a similar approach, but would take a few more lines.

Alternatively starting from v3, it's will also possible to provide the urls during initialization:

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

const provider = new OpenStreetMapProvider({
  searchUrl: 'https://example.com/api/search',
  reverseUrl: 'https://example.com/api/reverse',
});

map.addControl(new GeoSearchControl({
  provider,
}));
smeijer commented 4 years ago

I've updated the docs to include info about how to extend existing providers.

The docs are focused on the upcoming v3 release, but for v2 it should be quite similar. Looking at the current provider, you might want to override the endpoint method. If you also support reverse searches, endpointReverse should also be overridden.

import { OpenStreetMapProvider } from 'leaflet-geosearch';

class MyProvider extends OpenStreetMapProvider {
  endpoint({ query } = {}) {
    const { params } = this.options;

    const paramString = this.getParamString({
      ...params,
      format: 'json',
      q: query,
    });

    return `https://example.com/search?${paramString}`;
  }
}