Skip to content

Commit

Permalink
Support base URL configuration (ex: for EU endpoints)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthetechie committed Jul 7, 2023
1 parent be55db5 commit 636e165
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
## v0.5.0

- Adds arrow key-based navigation!

## v0.6.0

- Support changing the base URL (ex: for EU endpoints)
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class MapLibreSearchControlOptions {
minWaitPeriodMs = 100;
layers: PeliasLayer[] = null;
onResultSelected?: (feature: PeliasGeoJSONFeature) => void;
baseUrl: string | null = null;
}
```

Expand Down Expand Up @@ -150,6 +151,12 @@ results, use `['coarse']` for the best performance.
A callback to be invoked whenever a result is selected by the user. This is invoked with a single argument, the
`PeliasFeature` for the result. This allows you take an action (such as autofilling your own form).

### `baseUrl`

An optional override to the base API URL. This defaults to the primary Stadia Maps API endpoint. If you want
to use our [EU endpoints](https://docs.stadiamaps.com/eu-gdpr-endpoints/) to ensure traffic is handled by EU servers,
set the `baseUrl` to `https://api-eu.stadiamaps.com`.

## Development

- `dev` - starts dev server
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stadiamaps/maplibre-search-box",
"version": "0.5.1",
"version": "0.6.0",
"homepage": "https://docs.stadiamaps.com/",
"repository": "https://github.com/stadiamaps/maplibre-search-box",
"license": "BSD-3-Clause",
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { IControl, Map } from "maplibre-gl";
import {
type AutocompleteRequest,
Configuration,
GeocodingApi,
PeliasGeoJSONFeature,
PeliasLayer,
Expand All @@ -18,6 +19,7 @@ export class MapLibreSearchControlOptions {
minWaitPeriodMs = 100;
layers: PeliasLayer[] = null;
onResultSelected?: (feature: PeliasGeoJSONFeature) => void;
baseUrl: string | null = null;
}

export class MapLibreSearchControl implements IControl {
Expand All @@ -28,17 +30,20 @@ export class MapLibreSearchControl implements IControl {
private input: HTMLInputElement | null = null;
private clearButton: HTMLElement | null = null;
private loadingSpinner: HTMLElement | null = null;
private api = new GeocodingApi();
private api: GeocodingApi;
private lastRequestAt = 0;
private lastRequestString = "";
private resultFeatures: PeliasGeoJSONFeature[] = [];
private selectedResultIndex: number | null = null;
private originalInput: string = "";
private originalInput = "";

options = new MapLibreSearchControlOptions();

constructor(options: Partial<MapLibreSearchControlOptions> = {}) {
this.options = Object.assign(new MapLibreSearchControlOptions(), options);
this.api = new GeocodingApi(
new Configuration({ basePath: options.baseUrl })
);
}

onAdd(map: Map): HTMLElement {
Expand Down

0 comments on commit 636e165

Please sign in to comment.