Skip to content

Commit

Permalink
Let the party begin (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrohnstock committed Mar 24, 2015
1 parent 74431e1 commit c2bdd1f
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 95 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015, Per Liedman (per@liedman.net)
Copyright (c) 2015, Mathias Rohnstock (m.rohnstock@gmail.com)

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

Expand Down
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
Leaflet Routing Machine / GraphHopper
=====================================
Leaflet Routing Machine / TomTom
================================

[![npm version](https://img.shields.io/npm/v/lrm-graphhopper.svg)](https://www.npmjs.com/package/lrm-graphhopper)

Extends [Leaflet Routing Machine](https://github.com/perliedman/leaflet-routing-machine) with support for [GraphHopper](https://graphhopper.com/).
Extends [Leaflet Routing Machine](https://github.com/perliedman/leaflet-routing-machine) with support for [TomTom](http://developer.tomtom.com/io-docs).

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.

## Installing

Go to the [download page](http://www.liedman.net/lrm-graphhopper/download/) to get the script to include in your page. Put the script after Leaflet and Leaflet Routing Machine has been loaded.
Install nodejs/iojs, clone this repository and execute:

```sh
npm install
./scripts/dist.sh
```

Put the script after Leaflet and Leaflet Routing Machine has been loaded.

To use with for example Browserify:

```sh
npm install --save lrm-graphhopper
npm install --save lrm-tomtom
```

There's not pre-built files yet, but I will get to it.

## Using

There's a single class exported by this module, `L.Routing.GraphHopper`. It implements the [`IRouter`](http://www.liedman.net/leaflet-routing-machine/api/#irouter) interface. Use it to replace Leaflet Routing Machine's default OSRM router implementation:
There's a single class exported by this module, `L.Routing.TomTom`. It implements the [`IRouter`](http://www.liedman.net/leaflet-routing-machine/api/#irouter) interface. Use it to replace Leaflet Routing Machine's default OSRM router implementation:

```javascript
var L = require('leaflet');
require('leaflet-routing-machine');
require('lrm-graphhopper'); // This will tack on the class to the L.Routing namespace
require('lrm-tomtom'); // This will tack on the class to the L.Routing namespace

L.Routing.control({
router: new L.Routing.GraphHopper('your GraphHopper API key'),
router: new L.Routing.TomTom('your TomTom API key'),
}).addTo(map);
```

Note that you will need to pass a valid GraphHopper API key to the constructor.
Note that you will need to pass a valid TomTom API key to the constructor.
2 changes: 1 addition & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div id="map" class="map"></div>
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<script src="https://rawgit.com/perliedman/leaflet-routing-machine/master/dist/leaflet-routing-machine.js"></script>
<script src="../dist/lrm-graphhopper.js"></script>
<script src="../dist/lrm-tomtom.js"></script>
<script src="Control.Geocoder.js"></script>
<script src="index.js"></script>
</body>
Expand Down
2 changes: 1 addition & 1 deletion examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ L.Routing.control({
L.latLng(57.6792, 11.949)
],
geocoder: L.Control.Geocoder.nominatim(),
router: L.Routing.graphHopper('your-api-key'),
router: L.Routing.tomTom('your-api-key'),
routeWhileDragging: false
}).addTo(map);
21 changes: 10 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "lrm-graphhopper",
"version": "1.0.3",
"description": "Support for GraphHopper in Leaflet Routing Machine",
"main": "src/L.Routing.GraphHopper.js",
"name": "lrm-tomtom",
"version": "1.0.0",
"description": "Support for TomTom in Leaflet Routing Machine",
"main": "src/L.Routing.TomTom.js",
"scripts": {
"dist": "./scripts/dist.sh",
"publish": "./scripts/publish.sh",
Expand All @@ -11,20 +11,19 @@
"keywords": [
"leaflet",
"routing",
"graphhopper"
"tomtom"
],
"author": "Per Liedman <per@liedman.net>",
"author": "Mathias Rohnstock <m.rohnstock@gmail.com>",
"license": "ISC",
"repository": {
"type": "git",
"url": "https://github.com/perliedman/lrm-graphhopper.git"
"url": "https://github.com/drmonty/lrm-tomtom.git"
},
"homepage": "https://github.com/perliedman/lrm-graphhopper",
"bugs": "https://github.com/perliedman/lrm-graphhopper/issues",
"homepage": "https://github.com/drmonty/lrm-tomtom",
"bugs": "https://github.com/drmonty/lrm-tomtom/issues",
"dependencies": {
"corslite": "0.0.6",
"leaflet": "^0.7.3",
"polyline": "0.0.3"
"leaflet": "^0.7.3"
},
"browserify-shim": {
"leaflet": "global:L"
Expand Down
4 changes: 2 additions & 2 deletions scripts/dist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ VERSION=`echo "console.log(require('./package.json').version)" | node`

echo Building dist files for $VERSION...
mkdir -p dist
browserify -t browserify-shim src/L.Routing.GraphHopper.js >dist/lrm-graphhopper.js
uglifyjs dist/lrm-graphhopper.js >dist/lrm-graphhopper.min.js
browserify -t browserify-shim src/L.Routing.TomTom.js >dist/lrm-tomtom.js
uglifyjs dist/lrm-tomtom.js >dist/lrm-tomtom.min.js
echo Done.
4 changes: 2 additions & 2 deletions scripts/publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ git rebase origin/gh-pages

mkdir -p dist
mkdir -p _data
cp -a $CWD/dist/lrm-graphhopper.js dist/lrm-graphhopper-$VERSION.js
cp -a $CWD/dist/lrm-graphhopper.min.js dist/lrm-graphhopper-$VERSION.min.js
cp -a $CWD/dist/lrm-tomtom.js dist/lrm-tomtom-$VERSION.js
cp -a $CWD/dist/lrm-tomtom.min.js dist/lrm-tomtom-$VERSION.min.js
echo -e "- version: $VERSION\n" >>_data/versions.yml

echo `pwd`
Expand Down
88 changes: 24 additions & 64 deletions src/L.Routing.GraphHopper.js → src/L.Routing.TomTom.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

var L = require('leaflet');
var corslite = require('corslite');
var polyline = require('polyline');

L.Routing = L.Routing || {};

L.Routing.GraphHopper = L.Class.extend({
L.Routing.TomTom = L.Class.extend({
options: {
serviceUrl: 'https://graphhopper.com/api/1/route',
serviceUrl: 'https://api.tomtom.com/routing/1/calculateRoute',
timeout: 30 * 1000
},

Expand All @@ -33,7 +32,7 @@
timedOut = true;
callback.call(context || callback, {
status: -1,
message: 'GraphHopper request timed out.'
message: 'TomTom request timed out.'
});
}, this.options.timeout);

Expand Down Expand Up @@ -77,17 +76,16 @@
path;

context = context || callback;
if (response.info.errors && response.info.errors.length) {
if (response.error && response.error.description) {
callback.call(context, {
// TODO: include all errors
status: response.info.errors[0].details,
message: response.info.errors[0].message
status: response.statusCode,
message: response.error.description
});
return;
}

for (i = 0; i < response.paths.length; i++) {
path = response.paths[i];
for (i = 0; i < response.routes[0].legs.length; i++) {
path = response.routes[0].legs[i];
coordinates = this._decodePolyline(path.points);
mappedWaypoints =
this._mapWaypointIndices(inputWaypoints, path.instructions, coordinates);
Expand All @@ -97,8 +95,8 @@
coordinates: coordinates,
instructions: this._convertInstructions(path.instructions),
summary: {
totalDistance: path.distance,
totalTime: path.time / 1000,
totalDistance: path.summary.lengthInMeters,
totalTime: path.summary.travelTimeInSeconds / 1000,
},
inputWaypoints: inputWaypoints,
actualWaypoints: mappedWaypoints.waypoints,
Expand All @@ -110,11 +108,11 @@
},

_decodePolyline: function(geometry) {
var coords = polyline.decode(geometry, 5),
var coords = geometry,
latlngs = new Array(coords.length),
i;
for (i = 0; i < coords.length; i++) {
latlngs[i] = new L.LatLng(coords[i][0], coords[i][1]);
latlngs[i] = new L.LatLng(coords[i].latitude, coords[i].longitude);
}

return latlngs;
Expand All @@ -135,50 +133,23 @@
},

buildRouteUrl: function(waypoints, options) {
var computeInstructions =
!(options && options.geometryOnly),
locs = [],
var locs = [],
i;

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

return this.options.serviceUrl + '?' +
locs.join('&') +
'&instructions=' + computeInstructions +
'&type=json' +
'&key=' + this._apiKey;
return this.options.serviceUrl + '/' +
locs.join(':') +
'/jsonp' +
'?key=' + this._apiKey;
},

_convertInstructions: function(instructions) {
var signToType = {
'-3': 'SharpLeft',
'-2': 'Left',
'-1': 'SlightLeft',
0: 'Straight',
1: 'SlightRight',
2: 'Right',
3: 'SharpRight',
4: 'DestinationReached',
5: 'WaypointReached',
6: 'Roundabout'
},
result = [],
i,
instr;

for (i = 0; i < instructions.length; i++) {
instr = instructions[i];
result.push({
type: signToType[instr.sign],
text: instr.text,
distance: instr.distance,
time: instr.time / 1000,
index: instr.interval[0],
exit: instr.exit_number
});
}
var result = [];

// tomtom don't provide any instructions :(

return result;
},
Expand All @@ -192,17 +163,6 @@
wpIndices.push(0);
wps.push(new L.Routing.Waypoint(coordinates[0], waypoints[0].name));

for (i = 0; i < instructions.length; i++) {
if (instructions[i].sign === 5) { // VIA_REACHED
idx = instructions[i].interval[0];
wpIndices.push(idx);
wps.push({
latLng: coordinates[idx],
name: waypoints[wps.length + 1].name
});
}
}

wpIndices.push(coordinates.length - 1);
wps.push({
latLng: coordinates[coordinates.length - 1],
Expand All @@ -216,9 +176,9 @@
}
});

L.Routing.graphHopper = function(apiKey, options) {
return new L.Routing.GraphHopper(apiKey, options);
L.Routing.tomTom = function(apiKey, options) {
return new L.Routing.TomTom(apiKey, options);
};

module.exports = L.Routing.GraphHopper;
module.exports = L.Routing.TomTom;
})();

0 comments on commit c2bdd1f

Please sign in to comment.