Skip to content

Commit bbeb62e

Browse files
committed
Add support for Plus codes
https://plus.codes/ Requires peer dependency https://www.npmjs.com/package/open-location-code
1 parent 9636bfe commit bbeb62e

File tree

6 files changed

+70
-0
lines changed

6 files changed

+70
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The plugin supports many different data providers:
2424
* [Pelias](https://pelias.io/), [geocode.earth](https://geocode.earth/) (formerly Mapzen Search), [Openrouteservice](https://openrouteservice.org/dev/#/api-docs/geocode)
2525
* [HERE Geocoder API](https://developer.here.com/documentation/geocoder/topics/introduction.html)
2626
* [Neutrino API](https://www.neutrinoapi.com/api/geocode-address/)
27+
* [Plus codes](https://plus.codes/) (formerly OpenLocationCode) (requires [open-location-code](https://www.npmjs.com/package/open-location-code))
2728

2829
The plugin can easily be extended to support other providers. Current extensions:
2930

bower.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"what3words",
2424
"mapquest",
2525
"mapzen",
26+
"pluscodes",
27+
"open location code",
2628
"here"
2729
],
2830
"license": "BSD-2-Clause",

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
"what3words",
3636
"mapquest",
3737
"mapzen",
38+
"pluscodes",
39+
"open location code",
3840
"here"
3941
],
4042
"author": "Per Liedman <per@liedman.net>",
@@ -60,5 +62,8 @@
6062
"rollup-plugin-uglify": "^5.0.2",
6163
"sinon": "^7.3.2",
6264
"uglify-js": "^3.4.9"
65+
},
66+
"optionalDependencies": {
67+
"open-location-code": "^1.0.0"
6368
}
6469
}

src/geocoders/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export * from './mapbox';
77
export * from './mapquest';
88
export * from './neutrino';
99
export * from './nominatim';
10+
export * from './open-location-code';
1011
export * from './pelias';
1112
export * from './photon';
1213
export * from './what3words';
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import L from 'leaflet';
2+
3+
export var OpenLocationCode = L.Class.extend({
4+
options: {
5+
OpenLocationCode: undefined,
6+
codeLength: undefined
7+
},
8+
9+
initialize: function(options) {
10+
L.setOptions(this, options);
11+
},
12+
13+
geocode: function(query, cb, context) {
14+
try {
15+
var decoded = this.options.OpenLocationCode.decode(query);
16+
var result = {
17+
name: query,
18+
center: L.latLng(decoded.latitudeCenter, decoded.longitudeCenter),
19+
bbox: L.latLngBounds(
20+
L.latLng(decoded.latitudeLo, decoded.longitudeLo),
21+
L.latLng(decoded.latitudeHi, decoded.longitudeHi)
22+
)
23+
};
24+
cb.call(context, [result]);
25+
} catch (e) {
26+
console.warn(e); // eslint-disable-line no-console
27+
cb.call(context, []);
28+
}
29+
},
30+
reverse: function(location, scale, cb, context) {
31+
try {
32+
var code = this.options.OpenLocationCode.encode(
33+
location.lat,
34+
location.lng,
35+
this.options.codeLength
36+
);
37+
var result = {
38+
name: code,
39+
center: L.latLng(location.lat, location.lng),
40+
bbox: L.latLngBounds(
41+
L.latLng(location.lat, location.lng),
42+
L.latLng(location.lat, location.lng)
43+
)
44+
};
45+
cb.call(context, [result]);
46+
} catch (e) {
47+
console.warn(e); // eslint-disable-line no-console
48+
cb.call(context, []);
49+
}
50+
}
51+
});
52+
53+
export function openLocationCode(options) {
54+
return new OpenLocationCode(options);
55+
}

0 commit comments

Comments
 (0)