Skip to content

Commit d8932c1

Browse files
committed
V 1.1.0 - can calulate route using route restrictions
1 parent efee4d9 commit d8932c1

File tree

5 files changed

+127
-23
lines changed

5 files changed

+127
-23
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
Leaflet Routing Machine / HERE
22
=====================================
33

4-
54
Extends [Leaflet Routing Machine](https://github.com/perliedman/leaflet-routing-machine) with support for [Here](https://developer.here.com/rest-apis/documentation/routing/topics/overview.html) routing API.
65

76
Some brief instructions follow below, but the [Leaflet Routing Machine tutorial on alternative routers](http://www.liedman.net/leaflet-routing-machine/tutorials/alternative-routers/) is recommended.
@@ -29,4 +28,18 @@ L.Routing.control({
2928
Note that you will need to pass a valid Here app code and app id to the constructor.
3029

3130

32-
This is forked version based on trailbehind(https://github.com/trailbehind/lrm-Here)
31+
This is forked version based on [trailbehind](https://github.com/trailbehind/lrm-Here)
32+
33+
## RouteRestriction `routeRestriction`
34+
Since the 1.1.0 version, you can calculate the route witch attributes of the route. To achieve this set `generateMode: true` and fill options under `routeRestriction` object using properties from below.
35+
36+
| Property | Type | Default | Options |
37+
| ------ | ----- | ------- | ------- |
38+
| avoidHighways| boolean | | |
39+
| avoidTolls | boolean | | |
40+
| avoidFerries | boolean | | |
41+
| vehicleType | string | car | [Available options](https://developer.here.com/documentation/routing/topics/resource-param-type-routing-mode.html#type-transport-mode) |
42+
| routeType | string | fastest | [Available options](https://developer.here.com/documentation/routing/topics/resource-param-type-routing-mode.html#type-routing-type) |
43+
44+
### WARNING
45+
`generateMode` is by default `false` - Leaflet will calculate route using manually inserted mode (default `fastest;car;`)

dist/lrm-here.js

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ if (typeof module !== 'undefined' && module.exports) {
173173
serviceUrl: 'https://route.cit.api.here.com/routing/7.2/calculateroute.json',
174174
timeout: 30 * 1000,
175175
alternatives: 0,
176-
mode: 'fastest;car',
176+
mode: 'fastest;car;',
177+
generateMode: false,
177178
urlParameters: {}
178179
},
179180

@@ -280,7 +281,7 @@ if (typeof module !== 'undefined' && module.exports) {
280281
for(j = 0; j < path.waypoint.length; j++) {
281282
waypoint = path.waypoint[j];
282283
waypoints.push(new L.LatLng(
283-
waypoint.mappedPosition.latitude,
284+
waypoint.mappedPosition.latitude,
284285
waypoint.mappedPosition.longitude));
285286
}
286287

@@ -305,7 +306,7 @@ if (typeof module !== 'undefined' && module.exports) {
305306
coord,
306307
i;
307308
for (i = 0; i < geometry.length; i++) {
308-
coord = geometry[i].split(",");
309+
coord = geometry[i].split(',');
309310
latlngs[i] = ([parseFloat(coord[0]), parseFloat(coord[1])]);
310311
}
311312

@@ -317,24 +318,68 @@ if (typeof module !== 'undefined' && module.exports) {
317318
i,
318319
alternatives,
319320
baseUrl;
320-
321+
321322
for (i = 0; i < waypoints.length; i++) {
322323
locs.push('waypoint' + i + '=geo!' + waypoints[i].latLng.lat + ',' + waypoints[i].latLng.lng);
323324
}
324325

325-
alternatives = this.options.alternatives;
326+
alternatives = this.options.alternatives;
326327
baseUrl = this.options.serviceUrl + '?' + locs.join('&');
327328

328329
return baseUrl + L.Util.getParamString(L.extend({
329330
instructionFormat: 'text',
330331
app_code: this._appCode,
331332
app_id: this._appId,
332-
representation: "navigation",
333-
mode: this.options.mode,
333+
representation: 'navigation',
334+
mode: this._buildRouteMode(this.options),
334335
alternatives: alternatives
335336
}, this.options.urlParameters), baseUrl);
336337
},
337338

339+
_buildRouteMode: function(options) {
340+
if (options.generateMode === false) {
341+
return options.mode;
342+
}
343+
const modes = [];
344+
const avoidness = [];
345+
const avoidnessLevel = '-3'; //strictExclude
346+
347+
if (options.hasOwnProperty('routeRestriction')
348+
&& options.routeRestriction.hasOwnProperty('routeType')) {
349+
modes.push(options.routeRestriction.routeType); }
350+
else {
351+
modes.push('fastest');
352+
}
353+
354+
if (!options.hasOwnProperty('routeRestriction')
355+
&& options.routeRestriction.hasOwnProperty('vehicleType')) {
356+
modes.push(options.routeRestriction.vehicleType);
357+
} else {
358+
modes.push('car');
359+
}
360+
361+
if (options.hasOwnProperty('routeRestriction')
362+
&& options.routeRestriction.hasOwnProperty('avoidHighways')
363+
&& options.routeRestriction.avoidHighways === true) {
364+
avoidness.push('motorway:' + avoidnessLevel);
365+
}
366+
367+
if (options.hasOwnProperty('routeRestriction')
368+
&& options.routeRestriction.hasOwnProperty('avoidTolls')
369+
&& options.routeRestriction.avoidTolls === true) {
370+
avoidness.push('tollroad:' + avoidnessLevel);
371+
}
372+
373+
if (options.hasOwnProperty('routeRestriction')
374+
&& options.routeRestriction.hasOwnProperty('avoidFerries')
375+
&& options.routeRestriction.avoidFerries === true) {
376+
avoidness.push('boatFerry:' + avoidnessLevel);
377+
}
378+
379+
modes.push(avoidness.join(','));
380+
return modes.join(';');
381+
},
382+
338383
_convertInstruction: function(instruction, coordinates, startingSearchIndex) {
339384
var i,
340385
distance,
@@ -362,7 +407,7 @@ if (typeof module !== 'undefined' && module.exports) {
362407
},
363408

364409
});
365-
410+
366411
L.Routing.here = function(appId, appCode, options) {
367412
return new L.Routing.Here(appId, appCode, options);
368413
};

0 commit comments

Comments
 (0)