Skip to content

Commit

Permalink
Substantially improve Photon results with map-derived parameters. (#339)
Browse files Browse the repository at this point in the history
The Photon geocoder returns poor results unless the center point and
zoom of the map are included as query parameters.
  • Loading branch information
shivak authored Dec 26, 2024
1 parent 44628b1 commit 66e361e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,10 @@ export class GeocoderControl extends EventedControl {
const event: StartGeocodeEvent = { input: value };
this.fire(suggest ? 'startsuggest' : 'startgeocode', event);

const context = { map: this._map };
const results = suggest
? await this.options.geocoder!.suggest!(value)
: await this.options.geocoder!.geocode(value);
? await this.options.geocoder!.suggest!(value, context)

Check failure on line 321 in src/control.ts

View workflow job for this annotation

GitHub Actions / build

Expected 1 arguments, but got 2.
: await this.options.geocoder!.geocode(value, context);

Check failure on line 322 in src/control.ts

View workflow job for this annotation

GitHub Actions / build

Expected 1 arguments, but got 2.

if (requestCount === this._requestCount) {
const event: FinishGeocodeEvent = { input: value, results };
Expand Down
11 changes: 10 additions & 1 deletion src/geocoders/photon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ export class Photon implements IGeocoder {
L.Util.setOptions(this, options);
}

async geocode(query: string): Promise<GeocodingResult[]> {
async geocode(query: string, context?: { map?: L.Map }): Promise<GeocodingResult[]> {
const params = geocodingParams(this.options, { q: query });
const center = context?.map?.getCenter?.();
if (center) {
params.lat = center.lat;
params.lon = center.lng;
}
const zoom = context?.map?.getZoom?.();
if (zoom) {
params.zoom = zoom;
}
const data = await getJSON<any>(this.options.serviceUrl, params);
return this._parseResults(data);
}
Expand Down

0 comments on commit 66e361e

Please sign in to comment.