Skip to content

Commit

Permalink
Use reverseQueryParams in all geocoders
Browse files Browse the repository at this point in the history
  • Loading branch information
simon04 committed Nov 25, 2020
1 parent 84d764a commit 9240331
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 109 deletions.
7 changes: 7 additions & 0 deletions src/geocoders/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ export function geocodingParams(
): Record<string, unknown> {
return L.Util.extend(params, options.geocodingQueryParams);
}

export function reverseParams(
options: GeocoderOptions,
params: Record<string, unknown>
): Record<string, unknown> {
return L.Util.extend(params, options.reverseQueryParams);
}
8 changes: 4 additions & 4 deletions src/geocoders/arcgis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
GeocodingCallback,
geocodingParams,
GeocodingResult,
ReverseGeocodingResult
ReverseGeocodingResult,
reverseParams
} from './api';

export interface ArcGisOptions extends GeocoderOptions {}
Expand Down Expand Up @@ -63,12 +64,11 @@ export class ArcGis implements GeocoderAPI {
cb: (result: any) => void,
context?: any
): void {
const params = {
const params = reverseParams(this.options, {
location: encodeURIComponent(location.lng) + ',' + encodeURIComponent(location.lat),
distance: 100,
f: 'json'
};

});
getJSON(this.options.serviceUrl + '/reverseGeocode', params, data => {
const result: ReverseGeocodingResult[] = [];
if (data && !data.error) {
Expand Down
9 changes: 6 additions & 3 deletions src/geocoders/bing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
GeocodingResult,
reverseParams
} from './api';

export interface BingOptions extends GeocoderOptions {}
Expand Down Expand Up @@ -53,10 +54,12 @@ export class Bing implements GeocoderAPI {
cb: (result: any) => void,
context?: any
): void {
const params = { key: this.options.apiKey };
const params = reverseParams(this.options, {
key: this.options.apiKey
});
jsonp(
this.options.serviceUrl + location.lat + ',' + location.lng,
L.Util.extend(params, this.options.reverseQueryParams),
params,
data => {
const results: GeocodingResult[] = [];
for (let i = data.resourceSets[0].resources.length - 1; i >= 0; i--) {
Expand Down
9 changes: 4 additions & 5 deletions src/geocoders/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
GeocodingResult,
reverseParams
} from './api';

export interface GoogleOptions extends GeocoderOptions {}
Expand Down Expand Up @@ -53,12 +54,10 @@ export class Google implements GeocoderAPI {
cb: (result: any) => void,
context?: any
): void {
let params = {
const params = reverseParams(this.options, {
key: this.options.apiKey,
latlng: encodeURIComponent(location.lat) + ',' + encodeURIComponent(location.lng)
};
params = L.Util.extend(params, this.options.reverseQueryParams);

});
getJSON(this.options.serviceUrl, params, data => {
const results: GeocodingResult[] = [];
if (data.results && data.results.length) {
Expand Down
8 changes: 4 additions & 4 deletions src/geocoders/here.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
GeocodingResult,
reverseParams
} from './api';

export interface HereOptions extends GeocoderOptions {
Expand Down Expand Up @@ -48,15 +49,14 @@ export class HERE implements GeocoderAPI {
? this.options.reverseGeocodeProxRadius
: null;
const proxRadius = _proxRadius ? ',' + encodeURIComponent(_proxRadius) : '';
let params = {
const params = reverseParams(this.options, {
prox: encodeURIComponent(location.lat) + ',' + encodeURIComponent(location.lng) + proxRadius,
mode: 'retrieveAddresses',
app_id: this.options.app_id,
app_code: this.options.app_code,
gen: 9,
jsonattributes: 1
};
params = L.Util.extend(params, this.options.reverseQueryParams);
});
this.getJSON(this.options.serviceUrl + 'reversegeocode.json', params, cb, context);
}

Expand Down
10 changes: 5 additions & 5 deletions src/geocoders/mapbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
GeocodingResult,
reverseParams
} from './api';

export interface MapboxOptions extends GeocoderOptions {}
Expand Down Expand Up @@ -87,10 +88,9 @@ export class Mapbox implements GeocoderAPI {
cb: (result: any) => void,
context?: any
): void {
const param = L.Util.extend(
{ access_token: this.options.apiKey },
this.options.reverseQueryParams
);
const param = reverseParams(this.options, {
access_token: this.options.apiKey
});
getJSON(
this.options.serviceUrl +
encodeURIComponent(location.lng) +
Expand Down
14 changes: 8 additions & 6 deletions src/geocoders/mapquest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
GeocodingResult,
reverseParams
} from './api';

export interface MapQuestOptions extends GeocoderOptions {}
Expand Down Expand Up @@ -61,13 +62,14 @@ export class MapQuest implements GeocoderAPI {
cb: (result: any) => void,
context?: any
): void {
const params = reverseParams(this.options, {
key: this.options.apiKey,
location: location.lat + ',' + location.lng,
outputFormat: 'json'
});
getJSON(
this.options.serviceUrl + '/reverse',
{
key: this.options.apiKey,
location: location.lat + ',' + location.lng,
outputFormat: 'json'
},
params,
L.Util.bind(function(data) {
const results: GeocodingResult[] = [];
if (data.results && data.results[0].locations) {
Expand Down
42 changes: 20 additions & 22 deletions src/geocoders/neutrino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
GeocodingResult,
reverseParams
} from './api';

export interface NeutrinoOptions extends GeocoderOptions {
Expand Down Expand Up @@ -59,28 +60,25 @@ export class Neutrino implements GeocoderAPI {
cb: (result: any) => void,
context?: any
): void {
getJSON(
this.options.serviceUrl + 'geocode-reverse',
{
apiKey: this.options.apiKey,
userId: this.options.userId,
latitude: location.lat,
longitude: location.lng
},
data => {
const results: GeocodingResult[] = [];
if (data.status.status == 200 && data.found) {
const center = L.latLng(location.lat, location.lng);
const bbox = L.latLngBounds(center, center);
results[0] = {
name: data.address,
bbox: bbox,
center: center
};
}
cb.call(context, results);
const params = reverseParams(this.options, {
apiKey: this.options.apiKey,
userId: this.options.userId,
latitude: location.lat,
longitude: location.lng
});
getJSON(this.options.serviceUrl + 'geocode-reverse', params, data => {
const results: GeocodingResult[] = [];
if (data.status.status == 200 && data.found) {
const center = L.latLng(location.lat, location.lng);
const bbox = L.latLngBounds(center, center);
results[0] = {
name: data.address,
bbox: bbox,
center: center
};
}
);
cb.call(context, results);
});
}
}

Expand Down
53 changes: 24 additions & 29 deletions src/geocoders/nominatim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
GeocodingCallback,
geocodingParams,
GeocodingResult,
ReverseGeocodingResult
ReverseGeocodingResult,
reverseParams
} from './api';

export interface NominatimResult {
Expand Down Expand Up @@ -104,35 +105,29 @@ export class Nominatim implements GeocoderAPI {
}

reverse(location: L.LatLngLiteral, scale: number, cb: (result: any) => void, context?: any) {
getJSON(
this.options.serviceUrl + 'reverse',
L.Util.extend(
{
lat: location.lat,
lon: location.lng,
zoom: Math.round(Math.log(scale / 256) / Math.log(2)),
addressdetails: 1,
format: 'json'
},
this.options.reverseQueryParams
),
data => {
const result: ReverseGeocodingResult[] = [];
if (data && data.lat && data.lon) {
const center = L.latLng(data.lat, data.lon);
const bbox = L.latLngBounds(center, center);
result.push({
name: data.display_name,
html: this.options.htmlTemplate ? this.options.htmlTemplate(data) : undefined,
center: center,
bbox: bbox,
bounds: bbox,
properties: data
});
}
cb.call(context, result);
const params = reverseParams(this.options, {
lat: location.lat,
lon: location.lng,
zoom: Math.round(Math.log(scale / 256) / Math.log(2)),
addressdetails: 1,
format: 'json'
});
getJSON(this.options.serviceUrl + 'reverse', params, data => {
const result: ReverseGeocodingResult[] = [];
if (data && data.lat && data.lon) {
const center = L.latLng(data.lat, data.lon);
const bbox = L.latLngBounds(center, center);
result.push({
name: data.display_name,
html: this.options.htmlTemplate ? this.options.htmlTemplate(data) : undefined,
center: center,
bbox: bbox,
bounds: bbox,
properties: data
});
}
);
cb.call(context, result);
});
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/geocoders/opencage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
GeocodingResult,
reverseParams
} from './api';

export interface OpenCageOptions extends GeocoderOptions {}
Expand Down Expand Up @@ -60,11 +61,10 @@ export class OpenCage implements GeocoderAPI {
cb: (result: any) => void,
context?: any
): void {
let params = {
const params = reverseParams(this.options, {
key: this.options.apiKey,
q: [location.lat, location.lng].join(',')
};
params = L.Util.extend(params, this.options.reverseQueryParams);
});
getJSON(this.options.serviceUrl, params, data => {
const results: GeocodingResult[] = [];
if (data.results && data.results.length) {
Expand Down
25 changes: 10 additions & 15 deletions src/geocoders/pelias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
GeocodingResult,
reverseParams
} from './api';

export interface PeliasOptions extends GeocoderOptions {}
Expand Down Expand Up @@ -50,20 +51,14 @@ export class Pelias implements GeocoderAPI {
cb: (result: any) => void,
context?: any
): void {
getJSON(
this.options.serviceUrl + '/reverse',
L.Util.extend(
{
api_key: this.options.apiKey,
'point.lat': location.lat,
'point.lon': location.lng
},
this.options.reverseQueryParams
),
data => {
cb.call(context, this._parseResults(data, 'bounds'));
}
);
const params = reverseParams(this.options, {
api_key: this.options.apiKey,
'point.lat': location.lat,
'point.lon': location.lng
});
getJSON(this.options.serviceUrl + '/reverse', params, data => {
cb.call(context, this._parseResults(data, 'bounds'));
});
}

_parseResults(data, bboxname) {
Expand Down
15 changes: 6 additions & 9 deletions src/geocoders/photon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
GeocodingResult,
reverseParams
} from './api';

export interface PhotonOptions extends GeocoderOptions {
Expand Down Expand Up @@ -41,14 +42,10 @@ export class Photon implements GeocoderAPI {
}

reverse(latLng: L.LatLngLiteral, scale: number, cb: (result: any) => void, context?: any): void {
const params = L.Util.extend(
{
lat: latLng.lat,
lon: latLng.lng
},
this.options.reverseQueryParams
);

const params = reverseParams(this.options, {
lat: latLng.lat,
lon: latLng.lng
});
getJSON(
this.options.reverseUrl,
params,
Expand Down
Loading

0 comments on commit 9240331

Please sign in to comment.