diff --git a/LICENSE b/LICENSE index 50d8243..b636a1f 100644 --- a/LICENSE +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md index fdb0d0d..536958e 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/examples/index.html b/examples/index.html index b8bdc19..3a7f63d 100644 --- a/examples/index.html +++ b/examples/index.html @@ -11,7 +11,7 @@
- + diff --git a/examples/index.js b/examples/index.js index a9e2265..10b5869 100644 --- a/examples/index.js +++ b/examples/index.js @@ -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); diff --git a/package.json b/package.json index 5d98838..8184059 100644 --- a/package.json +++ b/package.json @@ -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", @@ -11,20 +11,19 @@ "keywords": [ "leaflet", "routing", - "graphhopper" + "tomtom" ], - "author": "Per Liedman ", + "author": "Mathias Rohnstock ", "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" diff --git a/scripts/dist.sh b/scripts/dist.sh index de1e261..342153d 100755 --- a/scripts/dist.sh +++ b/scripts/dist.sh @@ -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. diff --git a/scripts/publish.sh b/scripts/publish.sh index 4497c72..94d686b 100755 --- a/scripts/publish.sh +++ b/scripts/publish.sh @@ -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` diff --git a/src/L.Routing.GraphHopper.js b/src/L.Routing.TomTom.js similarity index 62% rename from src/L.Routing.GraphHopper.js rename to src/L.Routing.TomTom.js index 75c0254..e0e9aed 100644 --- a/src/L.Routing.GraphHopper.js +++ b/src/L.Routing.TomTom.js @@ -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 }, @@ -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); @@ -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); @@ -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, @@ -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; @@ -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; }, @@ -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], @@ -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; })();