Skip to content

Commit e4659b5

Browse files
committed
Harmonize options for all geocoders
BREAKING CHANGE: All geocoders accept an options object extending the interface GeocoderOptions. The apiKey string constructors of some geocoders have been removed.
1 parent 3398c8c commit e4659b5

21 files changed

+132
-155
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"env": { "browser": true },
1111
"rules": {
1212
"@typescript-eslint/explicit-module-boundary-types": "off",
13+
"@typescript-eslint/no-empty-interface": "off",
1314
"@typescript-eslint/no-explicit-any": "off",
1415
"prefer-arrow-callback": "warn",
1516
"prettier/prettier": "warn"

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ Note that you need an API key to use this service.
157157
### Constructor
158158

159159
```ts
160-
new L.Control.Geocoder.Bing(<String>key);
160+
new L.Control.Geocoder.Bing(options);
161161
// or
162-
L.Control.Geocoder.bing(<String>key);
162+
L.Control.Geocoder.bing(options);
163163
```
164164

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

173173
```ts
174-
new L.Control.Geocoder.OpenCage(<String>key, options);
174+
new L.Control.Geocoder.OpenCage(options);
175175
// or
176-
L.Control.Geocoder.opencage(<String>key, options);
176+
L.Control.Geocoder.opencage(options);
177177
```
178178

179179
### Options

spec/arcgis.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ArcGis } from '../src/geocoders/arcgis';
33

44
describe('L.Control.Geocoder.ArcGis', () => {
55
it('geocodes Innsbruck', () => {
6-
const geocoder = new ArcGis('');
6+
const geocoder = new ArcGis();
77
const callback = jest.fn();
88
testXMLHttpRequest(
99
'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?token=&SingleLine=Innsbruck&outFields=Addr_Type&forStorage=false&maxLocations=10&f=json',

spec/google.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { GeocodingResult } from '../src/geocoders/interfaces';
44

55
describe('L.Control.Geocoder.Google', () => {
66
it('geocodes Innsbruck', () => {
7-
const geocoder = new Google('0123xyz');
7+
const geocoder = new Google({ apiKey: '0123xyz' });
88
const callback = jest.fn();
99
testXMLHttpRequest(
1010
'https://maps.googleapis.com/maps/api/geocode/json?key=0123xyz&address=Innsbruck',

spec/mapbox.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Mapbox } from '../src/geocoders/mapbox';
33

44
describe('L.Control.Geocoder.Mapbox', () => {
55
it('geocodes Milwaukee Ave', () => {
6-
const geocoder = new Mapbox('0123');
6+
const geocoder = new Mapbox({ apiKey: '0123' });
77
const callback = jest.fn();
88
testXMLHttpRequest(
99
'https://api.mapbox.com/geocoding/v5/mapbox.places/Milwaukee%20Ave.json?access_token=0123',

spec/pelias.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { testXMLHttpRequest } from './mockXMLHttpRequest';
22
import { Openrouteservice } from '../src/geocoders/pelias';
33

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

77
it('geocodes Innsbruck', () => {
88
const callback = jest.fn();

src/control.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as L from 'leaflet';
22
import { Nominatim } from './geocoders/index';
33
import { GeocoderAPI, GeocodingResult } from './geocoders/interfaces';
44

5-
export interface GeocoderOptions extends L.ControlOptions {
5+
export interface GeocoderControlOptions extends L.ControlOptions {
66
collapsed: boolean;
77
expand: string;
88
placeholder: string;
@@ -19,7 +19,7 @@ export interface GeocoderOptions extends L.ControlOptions {
1919
}
2020

2121
export class GeocoderControl extends L.Control {
22-
options: GeocoderOptions = {
22+
options: GeocoderControlOptions = {
2323
showUniqueResult: true,
2424
showResultIcons: false,
2525
collapsed: true,
@@ -49,7 +49,7 @@ export class GeocoderControl extends L.Control {
4949
private _selection: any;
5050
private _suggestTimeout: any;
5151

52-
constructor(options?: Partial<GeocoderOptions>) {
52+
constructor(options?: Partial<GeocoderControlOptions>) {
5353
super(options);
5454
L.Util.setOptions(this, options);
5555
if (!this.options.geocoder) {
@@ -384,6 +384,6 @@ export class GeocoderControl extends L.Control {
384384
}
385385
}
386386

387-
export function geocoder(options?: Partial<GeocoderOptions>) {
387+
export function geocoder(options?: Partial<GeocoderControlOptions>) {
388388
return new GeocoderControl(options);
389389
}

src/geocoders/arcgis.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@ import * as L from 'leaflet';
22
import { getJSON } from '../util';
33
import {
44
GeocoderAPI,
5+
GeocoderOptions,
56
GeocodingCallback,
67
GeocodingResult,
78
ReverseGeocodingResult
89
} from './interfaces';
910

10-
export interface ArcGisOptions {
11-
geocodingQueryParams?: any;
12-
service_url: string;
13-
}
11+
export interface ArcGisOptions extends GeocoderOptions {}
1412

1513
export class ArcGis implements GeocoderAPI {
1614
options: ArcGisOptions = {
17-
service_url: 'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer'
15+
serviceUrl: 'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer',
16+
apiKey: ''
1817
};
1918

20-
constructor(private accessToken: string, options?: Partial<ArcGisOptions>) {
19+
constructor(options?: Partial<ArcGisOptions>) {
2120
L.Util.setOptions(this, options);
2221
}
2322

2423
geocode(query: string, cb: GeocodingCallback, context?: any): void {
2524
const params = {
26-
token: this.accessToken,
25+
token: this.options.apiKey,
2726
SingleLine: query,
2827
outFields: 'Addr_Type',
2928
forStorage: false,
@@ -32,7 +31,7 @@ export class ArcGis implements GeocoderAPI {
3231
};
3332

3433
getJSON(
35-
this.options.service_url + '/findAddressCandidates',
34+
this.options.serviceUrl + '/findAddressCandidates',
3635
L.Util.extend(params, this.options.geocodingQueryParams),
3736
data => {
3837
const results: GeocodingResult[] = [];
@@ -73,7 +72,7 @@ export class ArcGis implements GeocoderAPI {
7372
f: 'json'
7473
};
7574

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

94-
export function arcgis(accessToken: string, options?: Partial<ArcGisOptions>) {
95-
return new ArcGis(accessToken, options);
93+
export function arcgis(options?: Partial<ArcGisOptions>) {
94+
return new ArcGis(options);
9695
}

src/geocoders/bing.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
import * as L from 'leaflet';
22
import { jsonp } from '../util';
3-
import { GeocoderAPI, GeocodingCallback, GeocodingResult } from './interfaces';
3+
import { GeocoderAPI, GeocoderOptions, GeocodingCallback, GeocodingResult } from './interfaces';
4+
5+
export interface BingOptions extends GeocoderOptions {}
46

57
export class Bing implements GeocoderAPI {
6-
constructor(private key: string) {}
8+
options: BingOptions = {
9+
serviceUrl: 'https://dev.virtualearth.net/REST/v1/Locations'
10+
};
11+
12+
constructor(options?: Partial<BingOptions>) {
13+
L.Util.setOptions(this, options);
14+
}
715

816
geocode(query: string, cb: GeocodingCallback, context?: any): void {
17+
const params = {
18+
query: query,
19+
key: this.options.apiKey
20+
};
921
jsonp(
10-
'https://dev.virtualearth.net/REST/v1/Locations',
11-
{
12-
query: query,
13-
key: this.key
14-
},
22+
this.options.apiKey,
23+
L.Util.extend(params, this.options.geocodingQueryParams),
1524
data => {
1625
const results: GeocodingResult[] = [];
1726
if (data.resourceSets.length > 0) {
@@ -38,11 +47,10 @@ export class Bing implements GeocoderAPI {
3847
cb: (result: any) => void,
3948
context?: any
4049
): void {
50+
const params = { key: this.options.apiKey };
4151
jsonp(
42-
'//dev.virtualearth.net/REST/v1/Locations/' + location.lat + ',' + location.lng,
43-
{
44-
key: this.key
45-
},
52+
this.options.serviceUrl + location.lat + ',' + location.lng,
53+
L.Util.extend(params, this.options.reverseQueryParams),
4654
data => {
4755
const results: GeocodingResult[] = [];
4856
for (let i = data.resourceSets[0].resources.length - 1; i >= 0; i--) {
@@ -62,6 +70,6 @@ export class Bing implements GeocoderAPI {
6270
}
6371
}
6472

65-
export function bing(key: string) {
66-
return new Bing(key);
73+
export function bing(options?: Partial<BingOptions>) {
74+
return new Bing(options);
6775
}

src/geocoders/google.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11
import * as L from 'leaflet';
22
import { getJSON } from '../util';
3-
import { GeocoderAPI, GeocodingCallback, GeocodingResult } from './interfaces';
3+
import { GeocoderAPI, GeocoderOptions, GeocodingCallback, GeocodingResult } from './interfaces';
44

5-
export interface GoogleOptions {
6-
serviceUrl: string;
7-
geocodingQueryParams?: Record<string, unknown>;
8-
reverseQueryParams?: Record<string, unknown>;
9-
}
5+
export interface GoogleOptions extends GeocoderOptions {}
106

117
export class Google implements GeocoderAPI {
128
options: GoogleOptions = {
13-
serviceUrl: 'https://maps.googleapis.com/maps/api/geocode/json',
14-
geocodingQueryParams: {},
15-
reverseQueryParams: {}
9+
serviceUrl: 'https://maps.googleapis.com/maps/api/geocode/json'
1610
};
1711

18-
constructor(private key: string, options?: Partial<GoogleOptions>) {
12+
constructor(options?: Partial<GoogleOptions>) {
1913
L.Util.setOptions(this, options);
20-
// Backwards compatibility
21-
this.options.serviceUrl = (this.options as any).service_url || this.options.serviceUrl;
2214
}
2315

2416
geocode(query: string, cb: GeocodingCallback, context?: any): void {
2517
let params = {
26-
key: this.key,
18+
key: this.options.apiKey,
2719
address: query
2820
};
2921

@@ -59,7 +51,7 @@ export class Google implements GeocoderAPI {
5951
context?: any
6052
): void {
6153
let params = {
62-
key: this.key,
54+
key: this.options.apiKey,
6355
latlng: encodeURIComponent(location.lat) + ',' + encodeURIComponent(location.lng)
6456
};
6557
params = L.Util.extend(params, this.options.reverseQueryParams);
@@ -88,6 +80,6 @@ export class Google implements GeocoderAPI {
8880
}
8981
}
9082

91-
export function google(key: string, options?: Partial<GoogleOptions>) {
92-
return new Google(key, options);
83+
export function google(options?: Partial<GoogleOptions>) {
84+
return new Google(options);
9385
}

0 commit comments

Comments
 (0)