diff --git a/src/geocoders/api.ts b/src/geocoders/api.ts index 1cdae6c0..c96d2532 100644 --- a/src/geocoders/api.ts +++ b/src/geocoders/api.ts @@ -42,3 +42,10 @@ export function geocodingParams( ): Record { return L.Util.extend(params, options.geocodingQueryParams); } + +export function reverseParams( + options: GeocoderOptions, + params: Record +): Record { + return L.Util.extend(params, options.reverseQueryParams); +} diff --git a/src/geocoders/arcgis.ts b/src/geocoders/arcgis.ts index 92f1b29c..8b9fa962 100644 --- a/src/geocoders/arcgis.ts +++ b/src/geocoders/arcgis.ts @@ -6,7 +6,8 @@ import { GeocodingCallback, geocodingParams, GeocodingResult, - ReverseGeocodingResult + ReverseGeocodingResult, + reverseParams } from './api'; export interface ArcGisOptions extends GeocoderOptions {} @@ -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) { diff --git a/src/geocoders/bing.ts b/src/geocoders/bing.ts index 923acc2e..7d69e713 100644 --- a/src/geocoders/bing.ts +++ b/src/geocoders/bing.ts @@ -5,7 +5,8 @@ import { GeocoderOptions, GeocodingCallback, geocodingParams, - GeocodingResult + GeocodingResult, + reverseParams } from './api'; export interface BingOptions extends GeocoderOptions {} @@ -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--) { diff --git a/src/geocoders/google.ts b/src/geocoders/google.ts index 1078b687..ba39a981 100644 --- a/src/geocoders/google.ts +++ b/src/geocoders/google.ts @@ -5,7 +5,8 @@ import { GeocoderOptions, GeocodingCallback, geocodingParams, - GeocodingResult + GeocodingResult, + reverseParams } from './api'; export interface GoogleOptions extends GeocoderOptions {} @@ -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) { diff --git a/src/geocoders/here.ts b/src/geocoders/here.ts index 578003ae..fdc78f6d 100755 --- a/src/geocoders/here.ts +++ b/src/geocoders/here.ts @@ -5,7 +5,8 @@ import { GeocoderOptions, GeocodingCallback, geocodingParams, - GeocodingResult + GeocodingResult, + reverseParams } from './api'; export interface HereOptions extends GeocoderOptions { @@ -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); } diff --git a/src/geocoders/mapbox.ts b/src/geocoders/mapbox.ts index 18c8030e..978cfc29 100644 --- a/src/geocoders/mapbox.ts +++ b/src/geocoders/mapbox.ts @@ -5,7 +5,8 @@ import { GeocoderOptions, GeocodingCallback, geocodingParams, - GeocodingResult + GeocodingResult, + reverseParams } from './api'; export interface MapboxOptions extends GeocoderOptions {} @@ -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) + diff --git a/src/geocoders/mapquest.ts b/src/geocoders/mapquest.ts index 9fc9a674..9e9f5abc 100644 --- a/src/geocoders/mapquest.ts +++ b/src/geocoders/mapquest.ts @@ -5,7 +5,8 @@ import { GeocoderOptions, GeocodingCallback, geocodingParams, - GeocodingResult + GeocodingResult, + reverseParams } from './api'; export interface MapQuestOptions extends GeocoderOptions {} @@ -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) { diff --git a/src/geocoders/neutrino.ts b/src/geocoders/neutrino.ts index eba8b007..7dee4ef7 100644 --- a/src/geocoders/neutrino.ts +++ b/src/geocoders/neutrino.ts @@ -5,7 +5,8 @@ import { GeocoderOptions, GeocodingCallback, geocodingParams, - GeocodingResult + GeocodingResult, + reverseParams } from './api'; export interface NeutrinoOptions extends GeocoderOptions { @@ -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); + }); } } diff --git a/src/geocoders/nominatim.ts b/src/geocoders/nominatim.ts index 6603c70a..51d8d0e3 100644 --- a/src/geocoders/nominatim.ts +++ b/src/geocoders/nominatim.ts @@ -6,7 +6,8 @@ import { GeocodingCallback, geocodingParams, GeocodingResult, - ReverseGeocodingResult + ReverseGeocodingResult, + reverseParams } from './api'; export interface NominatimResult { @@ -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); + }); } } diff --git a/src/geocoders/opencage.ts b/src/geocoders/opencage.ts index 930f8c29..a9224ee4 100644 --- a/src/geocoders/opencage.ts +++ b/src/geocoders/opencage.ts @@ -5,7 +5,8 @@ import { GeocoderOptions, GeocodingCallback, geocodingParams, - GeocodingResult + GeocodingResult, + reverseParams } from './api'; export interface OpenCageOptions extends GeocoderOptions {} @@ -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) { diff --git a/src/geocoders/pelias.ts b/src/geocoders/pelias.ts index 597c4b38..3febead2 100644 --- a/src/geocoders/pelias.ts +++ b/src/geocoders/pelias.ts @@ -5,7 +5,8 @@ import { GeocoderOptions, GeocodingCallback, geocodingParams, - GeocodingResult + GeocodingResult, + reverseParams } from './api'; export interface PeliasOptions extends GeocoderOptions {} @@ -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) { diff --git a/src/geocoders/photon.ts b/src/geocoders/photon.ts index 889e8980..7734b936 100644 --- a/src/geocoders/photon.ts +++ b/src/geocoders/photon.ts @@ -5,7 +5,8 @@ import { GeocoderOptions, GeocodingCallback, geocodingParams, - GeocodingResult + GeocodingResult, + reverseParams } from './api'; export interface PhotonOptions extends GeocoderOptions { @@ -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, diff --git a/src/geocoders/what3words.ts b/src/geocoders/what3words.ts index de4ada4d..33cfe328 100644 --- a/src/geocoders/what3words.ts +++ b/src/geocoders/what3words.ts @@ -5,7 +5,8 @@ import { GeocoderOptions, GeocodingCallback, geocodingParams, - GeocodingResult + GeocodingResult, + reverseParams } from './api'; export interface What3WordsOptions extends GeocoderOptions {} @@ -56,10 +57,10 @@ export class What3Words implements GeocoderAPI { ): void { getJSON( this.options.serviceUrl + 'reverse', - { + reverseParams(this.options, { key: this.options.apiKey, coords: [location.lat, location.lng].join(',') - }, + }), data => { const results: GeocodingResult[] = []; if (data.status.status == 200) {