Skip to content

Commit

Permalink
Use geocodingQueryParams in all geocoders
Browse files Browse the repository at this point in the history
  • Loading branch information
simon04 committed Nov 25, 2020
1 parent 1013a03 commit 84d764a
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 146 deletions.
7 changes: 7 additions & 0 deletions src/geocoders/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ export interface GeocoderOptions {
reverseQueryParams?: Record<string, unknown>;
apiKey?: string;
}

export function geocodingParams(
options: GeocoderOptions,
params: Record<string, unknown>
): Record<string, unknown> {
return L.Util.extend(params, options.geocodingQueryParams);
}
45 changes: 21 additions & 24 deletions src/geocoders/arcgis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
GeocoderAPI,
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult,
ReverseGeocodingResult
} from './api';
Expand All @@ -21,39 +22,35 @@ export class ArcGis implements GeocoderAPI {
}

geocode(query: string, cb: GeocodingCallback, context?: any): void {
const params = {
const params = geocodingParams(this.options, {
token: this.options.apiKey,
SingleLine: query,
outFields: 'Addr_Type',
forStorage: false,
maxLocations: 10,
f: 'json'
};
});

getJSON(
this.options.serviceUrl + '/findAddressCandidates',
L.Util.extend(params, this.options.geocodingQueryParams),
data => {
const results: GeocodingResult[] = [];
if (data.candidates && data.candidates.length) {
for (let i = 0; i <= data.candidates.length - 1; i++) {
const loc = data.candidates[i];
const latLng = L.latLng(loc.location.y, loc.location.x);
const latLngBounds = L.latLngBounds(
L.latLng(loc.extent.ymax, loc.extent.xmax),
L.latLng(loc.extent.ymin, loc.extent.xmin)
);
results[i] = {
name: loc.address,
bbox: latLngBounds,
center: latLng
};
}
getJSON(this.options.serviceUrl + '/findAddressCandidates', params, data => {
const results: GeocodingResult[] = [];
if (data.candidates && data.candidates.length) {
for (let i = 0; i <= data.candidates.length - 1; i++) {
const loc = data.candidates[i];
const latLng = L.latLng(loc.location.y, loc.location.x);
const latLngBounds = L.latLngBounds(
L.latLng(loc.extent.ymax, loc.extent.xmax),
L.latLng(loc.extent.ymin, loc.extent.xmin)
);
results[i] = {
name: loc.address,
bbox: latLngBounds,
center: latLng
};
}

cb.call(context, results);
}
);

cb.call(context, results);
});
}

suggest(query: string, cb: GeocodingCallback, context?: any): void {
Expand Down
14 changes: 10 additions & 4 deletions src/geocoders/bing.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as L from 'leaflet';
import { jsonp } from '../util';
import { GeocoderAPI, GeocoderOptions, GeocodingCallback, GeocodingResult } from './api';
import {
GeocoderAPI,
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
} from './api';

export interface BingOptions extends GeocoderOptions {}

Expand All @@ -14,13 +20,13 @@ export class Bing implements GeocoderAPI {
}

geocode(query: string, cb: GeocodingCallback, context?: any): void {
const params = {
const params = geocodingParams(this.options, {
query: query,
key: this.options.apiKey
};
});
jsonp(
this.options.apiKey,
L.Util.extend(params, this.options.geocodingQueryParams),
params,
data => {
const results: GeocodingResult[] = [];
if (data.resourceSets.length > 0) {
Expand Down
15 changes: 9 additions & 6 deletions src/geocoders/google.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as L from 'leaflet';
import { getJSON } from '../util';
import { GeocoderAPI, GeocoderOptions, GeocodingCallback, GeocodingResult } from './api';
import {
GeocoderAPI,
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
} from './api';

export interface GoogleOptions extends GeocoderOptions {}

Expand All @@ -14,13 +20,10 @@ export class Google implements GeocoderAPI {
}

geocode(query: string, cb: GeocodingCallback, context?: any): void {
let params = {
const params = geocodingParams(this.options, {
key: this.options.apiKey,
address: query
};

params = L.Util.extend(params, this.options.geocodingQueryParams);

});
getJSON(this.options.serviceUrl, params, data => {
const results: GeocodingResult[] = [];
if (data.results && data.results.length) {
Expand Down
13 changes: 9 additions & 4 deletions src/geocoders/here.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as L from 'leaflet';
import { getJSON } from '../util';
import { GeocoderAPI, GeocoderOptions, GeocodingCallback, GeocodingResult } from './api';
import {
GeocoderAPI,
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
} from './api';

export interface HereOptions extends GeocoderOptions {
app_id: string;
Expand All @@ -22,14 +28,13 @@ export class HERE implements GeocoderAPI {
}

geocode(query: string, cb: GeocodingCallback, context?: any): void {
let params = {
const params = geocodingParams(this.options, {
searchtext: query,
gen: 9,
app_id: this.options.app_id,
app_code: this.options.app_code,
jsonattributes: 1
};
params = L.Util.extend(params, this.options.geocodingQueryParams);
});
this.getJSON(this.options.serviceUrl + 'geocode.json', params, cb, context);
}

Expand Down
15 changes: 10 additions & 5 deletions src/geocoders/mapbox.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as L from 'leaflet';
import { getJSON } from '../util';
import { GeocoderAPI, GeocoderOptions, GeocodingCallback, GeocodingResult } from './api';
import {
GeocoderAPI,
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
} from './api';

export interface MapboxOptions extends GeocoderOptions {}

Expand Down Expand Up @@ -32,10 +38,9 @@ export class Mapbox implements GeocoderAPI {
}

geocode(query: string, cb: GeocodingCallback, context?: any): void {
const params: any = L.Util.extend(
{ access_token: this.options.apiKey },
this.options.geocodingQueryParams
);
const params: any = geocodingParams(this.options, {
access_token: this.options.apiKey
});
if (
params.proximity !== undefined &&
params.proximity.lat !== undefined &&
Expand Down
21 changes: 14 additions & 7 deletions src/geocoders/mapquest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as L from 'leaflet';
import { getJSON } from '../util';
import { GeocoderAPI, GeocoderOptions, GeocodingCallback, GeocodingResult } from './api';
import {
GeocoderAPI,
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
} from './api';

export interface MapQuestOptions extends GeocoderOptions {}

Expand All @@ -21,14 +27,15 @@ export class MapQuest implements GeocoderAPI {
}

geocode(query: string, cb: GeocodingCallback, context?: any): void {
const params = geocodingParams(this.options, {
key: this.options.apiKey,
location: query,
limit: 5,
outFormat: 'json'
});
getJSON(
this.options.serviceUrl + '/address',
{
key: this.options.apiKey,
location: query,
limit: 5,
outFormat: 'json'
},
params,
L.Util.bind(function(data) {
const results: GeocodingResult[] = [];
if (data.results && data.results[0].locations) {
Expand Down
51 changes: 27 additions & 24 deletions src/geocoders/neutrino.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as L from 'leaflet';
import { getJSON } from '../util';
import { GeocoderAPI, GeocoderOptions, GeocodingCallback, GeocodingResult } from './api';
import {
GeocoderAPI,
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
} from './api';

export interface NeutrinoOptions extends GeocoderOptions {
userId: string;
Expand All @@ -19,30 +25,27 @@ export class Neutrino implements GeocoderAPI {

// https://www.neutrinoapi.com/api/geocode-address/
geocode(query: string, cb: GeocodingCallback, context?: any): void {
getJSON(
this.options.serviceUrl + 'geocode-address',
{
apiKey: this.options.apiKey,
userId: this.options.userId,
//get three words and make a dot based string
address: query.split(/\s+/).join('.')
},
data => {
const results: GeocodingResult[] = [];
if (data.locations) {
data.geometry = data.locations[0];
const center = L.latLng(data.geometry['latitude'], data.geometry['longitude']);
const bbox = L.latLngBounds(center, center);
results[0] = {
name: data.geometry.address,
bbox: bbox,
center: center
};
}

cb.call(context, results);
const params = geocodingParams(this.options, {
apiKey: this.options.apiKey,
userId: this.options.userId,
//get three words and make a dot based string
address: query.split(/\s+/).join('.')
});
getJSON(this.options.serviceUrl + 'geocode-address', params, data => {
const results: GeocodingResult[] = [];
if (data.locations) {
data.geometry = data.locations[0];
const center = L.latLng(data.geometry['latitude'], data.geometry['longitude']);
const bbox = L.latLngBounds(center, center);
results[0] = {
name: data.geometry.address,
bbox: bbox,
center: center
};
}
);

cb.call(context, results);
});
}

suggest(query: string, cb: GeocodingCallback, context?: any): void {
Expand Down
49 changes: 22 additions & 27 deletions src/geocoders/nominatim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
GeocoderAPI,
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult,
ReverseGeocodingResult
} from './api';
Expand Down Expand Up @@ -78,34 +79,28 @@ export class Nominatim implements GeocoderAPI {
}

geocode(query: string, cb: GeocodingCallback, context?: any) {
getJSON(
this.options.serviceUrl + 'search',
L.Util.extend(
{
q: query,
limit: 5,
format: 'json',
addressdetails: 1
},
this.options.geocodingQueryParams
),
data => {
const results: GeocodingResult[] = [];
for (let i = data.length - 1; i >= 0; i--) {
const bbox = data[i].boundingbox;
for (let j = 0; j < 4; j++) bbox[j] = parseFloat(bbox[j]);
results[i] = {
icon: data[i].icon,
name: data[i].display_name,
html: this.options.htmlTemplate ? this.options.htmlTemplate(data[i]) : undefined,
bbox: L.latLngBounds([bbox[0], bbox[2]], [bbox[1], bbox[3]]),
center: L.latLng(data[i].lat, data[i].lon),
properties: data[i]
};
}
cb.call(context, results);
const params = geocodingParams(this.options, {
q: query,
limit: 5,
format: 'json',
addressdetails: 1
});
getJSON(this.options.serviceUrl + 'search', params, data => {
const results: GeocodingResult[] = [];
for (let i = data.length - 1; i >= 0; i--) {
const bbox = data[i].boundingbox;
for (let j = 0; j < 4; j++) bbox[j] = parseFloat(bbox[j]);
results[i] = {
icon: data[i].icon,
name: data[i].display_name,
html: this.options.htmlTemplate ? this.options.htmlTemplate(data[i]) : undefined,
bbox: L.latLngBounds([bbox[0], bbox[2]], [bbox[1], bbox[3]]),
center: L.latLng(data[i].lat, data[i].lon),
properties: data[i]
};
}
);
cb.call(context, results);
});
}

reverse(location: L.LatLngLiteral, scale: number, cb: (result: any) => void, context?: any) {
Expand Down
16 changes: 11 additions & 5 deletions src/geocoders/opencage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as L from 'leaflet';
import { getJSON } from '../util';
import { GeocoderAPI, GeocoderOptions, GeocodingCallback, GeocodingResult } from './api';
import {
GeocoderAPI,
GeocoderOptions,
GeocodingCallback,
geocodingParams,
GeocodingResult
} from './api';

export interface OpenCageOptions extends GeocoderOptions {}

Expand All @@ -14,10 +20,10 @@ export class OpenCage implements GeocoderAPI {
}

geocode(query: string, cb: GeocodingCallback, context?: any): void {
const params = L.Util.extend(
{ key: this.options.apiKey, q: query },
this.options.geocodingQueryParams
);
const params = geocodingParams(this.options, {
key: this.options.apiKey,
q: query
});
getJSON(this.options.serviceUrl, params, data => {
const results: GeocodingResult[] = [];
if (data.results && data.results.length) {
Expand Down
Loading

0 comments on commit 84d764a

Please sign in to comment.