Skip to content

Commit

Permalink
Harmonize options for all geocoders
Browse files Browse the repository at this point in the history
BREAKING CHANGE: All geocoders accept an options object extending the interface GeocoderOptions. The apiKey string constructors of some geocoders have been removed.
  • Loading branch information
simon04 committed Nov 25, 2020
1 parent 3398c8c commit e4659b5
Show file tree
Hide file tree
Showing 21 changed files with 132 additions and 155 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"env": { "browser": true },
"rules": {
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-explicit-any": "off",
"prefer-arrow-callback": "warn",
"prettier/prettier": "warn"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ Note that you need an API key to use this service.
### Constructor

```ts
new L.Control.Geocoder.Bing(<String>key);
new L.Control.Geocoder.Bing(options);
// or
L.Control.Geocoder.bing(<String>key);
L.Control.Geocoder.bing(options);
```

## L.Control.Geocoder.OpenCage
Expand All @@ -171,9 +171,9 @@ Note that you need an API key to use this service.
### Constructor

```ts
new L.Control.Geocoder.OpenCage(<String>key, options);
new L.Control.Geocoder.OpenCage(options);
// or
L.Control.Geocoder.opencage(<String>key, options);
L.Control.Geocoder.opencage(options);
```

### Options
Expand Down
2 changes: 1 addition & 1 deletion spec/arcgis.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ArcGis } from '../src/geocoders/arcgis';

describe('L.Control.Geocoder.ArcGis', () => {
it('geocodes Innsbruck', () => {
const geocoder = new ArcGis('');
const geocoder = new ArcGis();
const callback = jest.fn();
testXMLHttpRequest(
'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?token=&SingleLine=Innsbruck&outFields=Addr_Type&forStorage=false&maxLocations=10&f=json',
Expand Down
2 changes: 1 addition & 1 deletion spec/google.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GeocodingResult } from '../src/geocoders/interfaces';

describe('L.Control.Geocoder.Google', () => {
it('geocodes Innsbruck', () => {
const geocoder = new Google('0123xyz');
const geocoder = new Google({ apiKey: '0123xyz' });
const callback = jest.fn();
testXMLHttpRequest(
'https://maps.googleapis.com/maps/api/geocode/json?key=0123xyz&address=Innsbruck',
Expand Down
2 changes: 1 addition & 1 deletion spec/mapbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Mapbox } from '../src/geocoders/mapbox';

describe('L.Control.Geocoder.Mapbox', () => {
it('geocodes Milwaukee Ave', () => {
const geocoder = new Mapbox('0123');
const geocoder = new Mapbox({ apiKey: '0123' });
const callback = jest.fn();
testXMLHttpRequest(
'https://api.mapbox.com/geocoding/v5/mapbox.places/Milwaukee%20Ave.json?access_token=0123',
Expand Down
2 changes: 1 addition & 1 deletion spec/pelias.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { testXMLHttpRequest } from './mockXMLHttpRequest';
import { Openrouteservice } from '../src/geocoders/pelias';

describe('L.Control.Geocoder.Openrouteservice', () => {
const geocoder = new Openrouteservice('0123', {});
const geocoder = new Openrouteservice({ apiKey: '0123' });

it('geocodes Innsbruck', () => {
const callback = jest.fn();
Expand Down
8 changes: 4 additions & 4 deletions src/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as L from 'leaflet';
import { Nominatim } from './geocoders/index';
import { GeocoderAPI, GeocodingResult } from './geocoders/interfaces';

export interface GeocoderOptions extends L.ControlOptions {
export interface GeocoderControlOptions extends L.ControlOptions {
collapsed: boolean;
expand: string;
placeholder: string;
Expand All @@ -19,7 +19,7 @@ export interface GeocoderOptions extends L.ControlOptions {
}

export class GeocoderControl extends L.Control {
options: GeocoderOptions = {
options: GeocoderControlOptions = {
showUniqueResult: true,
showResultIcons: false,
collapsed: true,
Expand Down Expand Up @@ -49,7 +49,7 @@ export class GeocoderControl extends L.Control {
private _selection: any;
private _suggestTimeout: any;

constructor(options?: Partial<GeocoderOptions>) {
constructor(options?: Partial<GeocoderControlOptions>) {
super(options);
L.Util.setOptions(this, options);
if (!this.options.geocoder) {
Expand Down Expand Up @@ -384,6 +384,6 @@ export class GeocoderControl extends L.Control {
}
}

export function geocoder(options?: Partial<GeocoderOptions>) {
export function geocoder(options?: Partial<GeocoderControlOptions>) {
return new GeocoderControl(options);
}
21 changes: 10 additions & 11 deletions src/geocoders/arcgis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@ import * as L from 'leaflet';
import { getJSON } from '../util';
import {
GeocoderAPI,
GeocoderOptions,
GeocodingCallback,
GeocodingResult,
ReverseGeocodingResult
} from './interfaces';

export interface ArcGisOptions {
geocodingQueryParams?: any;
service_url: string;
}
export interface ArcGisOptions extends GeocoderOptions {}

export class ArcGis implements GeocoderAPI {
options: ArcGisOptions = {
service_url: 'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer'
serviceUrl: 'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer',
apiKey: ''
};

constructor(private accessToken: string, options?: Partial<ArcGisOptions>) {
constructor(options?: Partial<ArcGisOptions>) {
L.Util.setOptions(this, options);
}

geocode(query: string, cb: GeocodingCallback, context?: any): void {
const params = {
token: this.accessToken,
token: this.options.apiKey,
SingleLine: query,
outFields: 'Addr_Type',
forStorage: false,
Expand All @@ -32,7 +31,7 @@ export class ArcGis implements GeocoderAPI {
};

getJSON(
this.options.service_url + '/findAddressCandidates',
this.options.serviceUrl + '/findAddressCandidates',
L.Util.extend(params, this.options.geocodingQueryParams),
data => {
const results: GeocodingResult[] = [];
Expand Down Expand Up @@ -73,7 +72,7 @@ export class ArcGis implements GeocoderAPI {
f: 'json'
};

getJSON(this.options.service_url + '/reverseGeocode', params, data => {
getJSON(this.options.serviceUrl + '/reverseGeocode', params, data => {
const result: ReverseGeocodingResult[] = [];
if (data && !data.error) {
const center = L.latLng(data.location.y, data.location.x);
Expand All @@ -91,6 +90,6 @@ export class ArcGis implements GeocoderAPI {
}
}

export function arcgis(accessToken: string, options?: Partial<ArcGisOptions>) {
return new ArcGis(accessToken, options);
export function arcgis(options?: Partial<ArcGisOptions>) {
return new ArcGis(options);
}
34 changes: 21 additions & 13 deletions src/geocoders/bing.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import * as L from 'leaflet';
import { jsonp } from '../util';
import { GeocoderAPI, GeocodingCallback, GeocodingResult } from './interfaces';
import { GeocoderAPI, GeocoderOptions, GeocodingCallback, GeocodingResult } from './interfaces';

export interface BingOptions extends GeocoderOptions {}

export class Bing implements GeocoderAPI {
constructor(private key: string) {}
options: BingOptions = {
serviceUrl: 'https://dev.virtualearth.net/REST/v1/Locations'
};

constructor(options?: Partial<BingOptions>) {
L.Util.setOptions(this, options);
}

geocode(query: string, cb: GeocodingCallback, context?: any): void {
const params = {
query: query,
key: this.options.apiKey
};
jsonp(
'https://dev.virtualearth.net/REST/v1/Locations',
{
query: query,
key: this.key
},
this.options.apiKey,
L.Util.extend(params, this.options.geocodingQueryParams),
data => {
const results: GeocodingResult[] = [];
if (data.resourceSets.length > 0) {
Expand All @@ -38,11 +47,10 @@ export class Bing implements GeocoderAPI {
cb: (result: any) => void,
context?: any
): void {
const params = { key: this.options.apiKey };
jsonp(
'//dev.virtualearth.net/REST/v1/Locations/' + location.lat + ',' + location.lng,
{
key: this.key
},
this.options.serviceUrl + location.lat + ',' + location.lng,
L.Util.extend(params, this.options.reverseQueryParams),
data => {
const results: GeocodingResult[] = [];
for (let i = data.resourceSets[0].resources.length - 1; i >= 0; i--) {
Expand All @@ -62,6 +70,6 @@ export class Bing implements GeocoderAPI {
}
}

export function bing(key: string) {
return new Bing(key);
export function bing(options?: Partial<BingOptions>) {
return new Bing(options);
}
24 changes: 8 additions & 16 deletions src/geocoders/google.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
import * as L from 'leaflet';
import { getJSON } from '../util';
import { GeocoderAPI, GeocodingCallback, GeocodingResult } from './interfaces';
import { GeocoderAPI, GeocoderOptions, GeocodingCallback, GeocodingResult } from './interfaces';

export interface GoogleOptions {
serviceUrl: string;
geocodingQueryParams?: Record<string, unknown>;
reverseQueryParams?: Record<string, unknown>;
}
export interface GoogleOptions extends GeocoderOptions {}

export class Google implements GeocoderAPI {
options: GoogleOptions = {
serviceUrl: 'https://maps.googleapis.com/maps/api/geocode/json',
geocodingQueryParams: {},
reverseQueryParams: {}
serviceUrl: 'https://maps.googleapis.com/maps/api/geocode/json'
};

constructor(private key: string, options?: Partial<GoogleOptions>) {
constructor(options?: Partial<GoogleOptions>) {
L.Util.setOptions(this, options);
// Backwards compatibility
this.options.serviceUrl = (this.options as any).service_url || this.options.serviceUrl;
}

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

Expand Down Expand Up @@ -59,7 +51,7 @@ export class Google implements GeocoderAPI {
context?: any
): void {
let params = {
key: this.key,
key: this.options.apiKey,
latlng: encodeURIComponent(location.lat) + ',' + encodeURIComponent(location.lng)
};
params = L.Util.extend(params, this.options.reverseQueryParams);
Expand Down Expand Up @@ -88,6 +80,6 @@ export class Google implements GeocoderAPI {
}
}

export function google(key: string, options?: Partial<GoogleOptions>) {
return new Google(key, options);
export function google(options?: Partial<GoogleOptions>) {
return new Google(options);
}
22 changes: 8 additions & 14 deletions src/geocoders/here.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
import * as L from 'leaflet';
import { getJSON } from '../util';
import { GeocoderAPI, GeocodingCallback, GeocodingResult } from './interfaces';
import { GeocoderAPI, GeocoderOptions, GeocodingCallback, GeocodingResult } from './interfaces';

export interface HereOptions {
geocodeUrl: string;
reverseGeocodeUrl: string;
export interface HereOptions extends GeocoderOptions {
app_id: string;
app_code: string;
geocodingQueryParams?: Record<string, unknown>;
reverseQueryParams?: Record<string, unknown>;
reverseGeocodeProxRadius: null;
}

export class HERE implements GeocoderAPI {
options: HereOptions = {
geocodeUrl: 'https://geocoder.api.here.com/6.2/geocode.json',
reverseGeocodeUrl: 'https://reverse.geocoder.api.here.com/6.2/reversegeocode.json',
app_id: '<insert your app_id here>',
app_code: '<insert your app_code here>',
geocodingQueryParams: {},
reverseQueryParams: {},
serviceUrl: 'https://geocoder.api.here.com/6.2/',
app_id: '',
app_code: '',
reverseGeocodeProxRadius: null
};

constructor(options?: Partial<HereOptions>) {
L.Util.setOptions(this, options);
if (options.apiKey) throw Error('apiKey is not supported, use app_id/app_code instead!');
}

geocode(query: string, cb: GeocodingCallback, context?: any): void {
Expand All @@ -36,7 +30,7 @@ export class HERE implements GeocoderAPI {
jsonattributes: 1
};
params = L.Util.extend(params, this.options.geocodingQueryParams);
this.getJSON(this.options.geocodeUrl, params, cb, context);
this.getJSON(this.options.serviceUrl + 'geocode.json', params, cb, context);
}

reverse(
Expand All @@ -58,7 +52,7 @@ export class HERE implements GeocoderAPI {
jsonattributes: 1
};
params = L.Util.extend(params, this.options.reverseQueryParams);
this.getJSON(this.options.reverseGeocodeUrl, params, cb, context);
this.getJSON(this.options.serviceUrl + 'reversegeocode.json', params, cb, context);
}

getJSON(url: string, params: any, cb: (result: any) => void, context?: any) {
Expand Down
1 change: 1 addition & 0 deletions src/geocoders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './arcgis';
export * from './bing';
export * from './google';
export * from './here';
export * from './interfaces';
export * from './latlng';
export * from './mapbox';
export * from './mapquest';
Expand Down
7 changes: 7 additions & 0 deletions src/geocoders/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ export interface GeocoderAPI {
context?: any
): void;
}

export interface GeocoderOptions {
serviceUrl: string;
geocodingQueryParams?: Record<string, unknown>;
reverseQueryParams?: Record<string, unknown>;
apiKey?: string;
}
Loading

0 comments on commit e4659b5

Please sign in to comment.