Skip to content

Commit

Permalink
V 1.1.0 - can calulate route using route restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
Daredzik committed Sep 5, 2019
1 parent efee4d9 commit d8932c1
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 23 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Leaflet Routing Machine / HERE
=====================================


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.

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.
Expand Down Expand Up @@ -29,4 +28,18 @@ L.Routing.control({
Note that you will need to pass a valid Here app code and app id to the constructor.


This is forked version based on trailbehind(https://github.com/trailbehind/lrm-Here)
This is forked version based on [trailbehind](https://github.com/trailbehind/lrm-Here)

## RouteRestriction `routeRestriction`
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.

| Property | Type | Default | Options |
| ------ | ----- | ------- | ------- |
| avoidHighways| boolean | | |
| avoidTolls | boolean | | |
| avoidFerries | boolean | | |
| vehicleType | string | car | [Available options](https://developer.here.com/documentation/routing/topics/resource-param-type-routing-mode.html#type-transport-mode) |
| routeType | string | fastest | [Available options](https://developer.here.com/documentation/routing/topics/resource-param-type-routing-mode.html#type-routing-type) |

### WARNING
`generateMode` is by default `false` - Leaflet will calculate route using manually inserted mode (default `fastest;car;`)
61 changes: 53 additions & 8 deletions dist/lrm-here.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ if (typeof module !== 'undefined' && module.exports) {
serviceUrl: 'https://route.cit.api.here.com/routing/7.2/calculateroute.json',
timeout: 30 * 1000,
alternatives: 0,
mode: 'fastest;car',
mode: 'fastest;car;',
generateMode: false,
urlParameters: {}
},

Expand Down Expand Up @@ -280,7 +281,7 @@ if (typeof module !== 'undefined' && module.exports) {
for(j = 0; j < path.waypoint.length; j++) {
waypoint = path.waypoint[j];
waypoints.push(new L.LatLng(
waypoint.mappedPosition.latitude,
waypoint.mappedPosition.latitude,
waypoint.mappedPosition.longitude));
}

Expand All @@ -305,7 +306,7 @@ if (typeof module !== 'undefined' && module.exports) {
coord,
i;
for (i = 0; i < geometry.length; i++) {
coord = geometry[i].split(",");
coord = geometry[i].split(',');
latlngs[i] = ([parseFloat(coord[0]), parseFloat(coord[1])]);
}

Expand All @@ -317,24 +318,68 @@ if (typeof module !== 'undefined' && module.exports) {
i,
alternatives,
baseUrl;

for (i = 0; i < waypoints.length; i++) {
locs.push('waypoint' + i + '=geo!' + waypoints[i].latLng.lat + ',' + waypoints[i].latLng.lng);
}

alternatives = this.options.alternatives;
alternatives = this.options.alternatives;
baseUrl = this.options.serviceUrl + '?' + locs.join('&');

return baseUrl + L.Util.getParamString(L.extend({
instructionFormat: 'text',
app_code: this._appCode,
app_id: this._appId,
representation: "navigation",
mode: this.options.mode,
representation: 'navigation',
mode: this._buildRouteMode(this.options),
alternatives: alternatives
}, this.options.urlParameters), baseUrl);
},

_buildRouteMode: function(options) {
if (options.generateMode === false) {
return options.mode;
}
const modes = [];
const avoidness = [];
const avoidnessLevel = '-3'; //strictExclude

if (options.hasOwnProperty('routeRestriction')
&& options.routeRestriction.hasOwnProperty('routeType')) {
modes.push(options.routeRestriction.routeType); }
else {
modes.push('fastest');
}

if (!options.hasOwnProperty('routeRestriction')
&& options.routeRestriction.hasOwnProperty('vehicleType')) {
modes.push(options.routeRestriction.vehicleType);
} else {
modes.push('car');
}

if (options.hasOwnProperty('routeRestriction')
&& options.routeRestriction.hasOwnProperty('avoidHighways')
&& options.routeRestriction.avoidHighways === true) {
avoidness.push('motorway:' + avoidnessLevel);
}

if (options.hasOwnProperty('routeRestriction')
&& options.routeRestriction.hasOwnProperty('avoidTolls')
&& options.routeRestriction.avoidTolls === true) {
avoidness.push('tollroad:' + avoidnessLevel);
}

if (options.hasOwnProperty('routeRestriction')
&& options.routeRestriction.hasOwnProperty('avoidFerries')
&& options.routeRestriction.avoidFerries === true) {
avoidness.push('boatFerry:' + avoidnessLevel);
}

modes.push(avoidness.join(','));
return modes.join(';');
},

_convertInstruction: function(instruction, coordinates, startingSearchIndex) {
var i,
distance,
Expand Down Expand Up @@ -362,7 +407,7 @@ if (typeof module !== 'undefined' && module.exports) {
},

});

L.Routing.here = function(appId, appCode, options) {
return new L.Routing.Here(appId, appCode, options);
};
Expand Down
Loading

0 comments on commit d8932c1

Please sign in to comment.