Skip to content

Commit 54d2692

Browse files
committed
Add support for Openrouteservice
Fixes #239.
1 parent 5a501d5 commit 54d2692

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The plugin supports many different data providers:
2121
* [MapQuest Geocoding API](http://developer.mapquest.com/web/products/dev-services/geocoding-ws)
2222
* [What3Words](http://what3words.com/)
2323
* [Photon](http://photon.komoot.de/)
24-
* [Pelias](https://pelias.io/), [geocode.earth](https://geocode.earth/) (formerly Mapzen Search)
24+
* [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/)
2727

spec/pelias.spec.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
describe('L.Control.Geocoder.Openrouteservice', function() {
2+
var server;
3+
var geocoder = new L.Control.Geocoder.Openrouteservice('0123');
4+
5+
beforeEach(function() {
6+
server = sinon.fakeServer.create();
7+
});
8+
afterEach(function() {
9+
server.restore();
10+
});
11+
12+
it('geocodes Innsbruck', function() {
13+
server.respondWith(
14+
'https://api.openrouteservice.org/geocode/search?api_key=0123&text=innsbruck',
15+
JSON.stringify({
16+
geocoding: {
17+
version: '0.2',
18+
attribution: 'openrouteservice.org | OpenStreetMap contributors | Geocoding by Pelias',
19+
query: {},
20+
warnings: ["performance optimization: excluding 'address' layer"],
21+
engine: { name: 'Pelias', author: 'Mapzen', version: '1.0' }
22+
},
23+
type: 'FeatureCollection',
24+
features: [
25+
{
26+
type: 'Feature',
27+
geometry: { type: 'Point', coordinates: [11.407851, 47.272308] },
28+
properties: {
29+
id: '101748061',
30+
layer: 'locality',
31+
source_id: '101748061',
32+
name: 'Innsbruck',
33+
confidence: 1,
34+
match_type: 'exact',
35+
accuracy: 'centroid',
36+
country: 'Austria',
37+
country_a: 'AUT',
38+
region: 'Tirol',
39+
region_a: 'TR',
40+
county: 'Innsbruck',
41+
county_a: 'IN',
42+
localadmin: 'Innsbruck',
43+
locality: 'Innsbruck',
44+
continent: 'Europe',
45+
label: 'Innsbruck, Austria'
46+
},
47+
bbox: [11.3218091258, 47.2470573997, 11.452584553, 47.29398]
48+
}
49+
],
50+
bbox: [10.9896885523, 46.9624806033, 11.7051690163, 47.4499185397]
51+
})
52+
);
53+
54+
var callback = sinon.fake();
55+
geocoder.geocode('innsbruck', callback);
56+
server.respond();
57+
58+
expect(callback.calledOnce).to.be.ok();
59+
expect(callback.lastArg).to.be.ok();
60+
expect(callback.lastArg.length).to.eql(1);
61+
expect(callback.lastArg[0].name).to.eql('Innsbruck, Austria');
62+
expect(callback.lastArg[0].center).to.eql({ lat: 47.272308, lng: 11.407851 });
63+
expect(callback.lastArg[0].bbox).to.eql(
64+
L.latLngBounds([
65+
{ lat: 47.2470573997, lng: 11.3218091258 },
66+
{ lat: 47.29398, lng: 11.452584553 }
67+
])
68+
);
69+
});
70+
});

src/geocoders/pelias.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ export var Pelias = L.Class.extend({
8383
if (layer.getBounds) {
8484
bbox = layer.getBounds();
8585
center = bbox.getCenter();
86+
} else if (layer.feature.bbox) {
87+
center = layer.getLatLng();
88+
bbox = L.latLngBounds(
89+
L.GeoJSON.coordsToLatLng(layer.feature.bbox.slice(0, 2)),
90+
L.GeoJSON.coordsToLatLng(layer.feature.bbox.slice(2, 4))
91+
);
8692
} else {
8793
center = layer.getLatLng();
8894
bbox = L.latLngBounds(center, center);
@@ -107,3 +113,12 @@ export var geocodeEarth = pelias;
107113

108114
export var Mapzen = Pelias; // r.i.p.
109115
export var mapzen = pelias;
116+
117+
export var Openrouteservice = Mapzen.extend({
118+
options: {
119+
serviceUrl: 'https://api.openrouteservice.org/geocode'
120+
}
121+
});
122+
export function openrouteservice(apiKey, options) {
123+
return new Openrouteservice(apiKey, options);
124+
}

0 commit comments

Comments
 (0)