Skip to content

Commit

Permalink
Substantially improve Photon results with map-derived parameters.
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 and simon04 committed Dec 26, 2024
1 parent 44628b1 commit 950dd75
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 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)
: await this.options.geocoder!.geocode(value, context);

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

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

0 comments on commit 950dd75

Please sign in to comment.