diff --git a/package.json b/package.json index 8184059..e1bcf62 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,6 @@ "main": "src/L.Routing.TomTom.js", "scripts": { "dist": "./scripts/dist.sh", - "publish": "./scripts/publish.sh", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ diff --git a/scripts/publish.sh b/scripts/publish.sh deleted file mode 100755 index 94d686b..0000000 --- a/scripts/publish.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash - -VERSION=`echo "console.log(require('./package.json').version)" | node` -ORIGIN=`git remote -v|grep origin|head -n1|cut -f2|cut -d" " -f1` -TMP=/tmp/.gh-pages-update -CWD=`pwd` - -./scripts/dist.sh - -echo Updating dist files on gh-pages... -rm -rf $TMP -git clone -b gh-pages . $TMP -cd $TMP -git remote set-url origin $ORIGIN -git fetch origin gh-pages -git rebase origin/gh-pages - -mkdir -p dist -mkdir -p _data -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` -git add -f dist/ _data/ -git commit -m "Dist files $VERSION" -git push origin gh-pages -cd $CWD -rm -rf $TMP diff --git a/src/L.Routing.TomTom.js b/src/L.Routing.TomTom.js index e0e9aed..5bf678e 100644 --- a/src/L.Routing.TomTom.js +++ b/src/L.Routing.TomTom.js @@ -70,15 +70,18 @@ _routeDone: function(response, inputWaypoints, callback, context) { var alts = [], - mappedWaypoints, - coordinates, - i, - path; + mappedWaypoints, + coordinates = [], + i, + path, + summary = [], + instructions, + index = 0; context = context || callback; if (response.error && response.error.description) { callback.call(context, { - status: response.statusCode, + status: -1, message: response.error.description }); return; @@ -86,24 +89,24 @@ 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); - - alts.push({ - name: '', - coordinates: coordinates, - instructions: this._convertInstructions(path.instructions), - summary: { - totalDistance: path.summary.lengthInMeters, - totalTime: path.summary.travelTimeInSeconds / 1000, - }, - inputWaypoints: inputWaypoints, - actualWaypoints: mappedWaypoints.waypoints, - waypointIndices: mappedWaypoints.waypointIndices - }); + coordinates = coordinates.concat(this._decodePolyline(path.points)); + index += (path.points.length - 1); + summary.push({ summary: path.summary, index: index }); } + instructions = this._convertInstructions(summary); + mappedWaypoints = this._mapWaypointIndices(inputWaypoints, instructions, coordinates); + + alts = [{ + name: '', + coordinates: coordinates, + instructions: instructions, + summary: this._convertSummary(summary), + inputWaypoints: inputWaypoints, + actualWaypoints: mappedWaypoints.waypoints, + waypointIndices: mappedWaypoints.waypointIndices + }]; + callback.call(context, null, alts); }, @@ -111,6 +114,7 @@ var coords = geometry, latlngs = new Array(coords.length), i; + for (i = 0; i < coords.length; i++) { latlngs[i] = new L.LatLng(coords[i].latitude, coords[i].longitude); } @@ -120,7 +124,7 @@ _toWaypoints: function(inputWaypoints, vias) { var wps = [], - i; + i; for (i = 0; i < vias.length; i++) { wps.push({ latLng: L.latLng(vias[i]), @@ -142,14 +146,35 @@ return this.options.serviceUrl + '/' + locs.join(':') + - '/jsonp' + + '/json' + '?key=' + this._apiKey; }, - _convertInstructions: function(instructions) { - var result = []; + _convertInstructions: function(summaries) { + var result = [], + i; - // tomtom don't provide any instructions :( + // tomtom don't provide any instructions, but we will create instructions from summary + for (i = 0; i < summaries.length; i++) + { + result.push({ distance: summaries[i].summary.lengthInMeters, + time: summaries[i].summary.travelTimeInSeconds, + type: (i == summaries.length - 1 ? "DestinationReached" : "WaypointReached"), + index: summaries[i].index }); + } + + return result; + }, + + _convertSummary: function(summaries) { + var result = { totalDistance: 0, + totalTime: 0 }, + i; + + for (i = 0; i < summaries.length; i++) { + result.totalDistance += summaries[i].summary.lengthInMeters; + result.totalTime += summaries[i].summary.travelTimeInSeconds; + } return result; }, @@ -157,12 +182,23 @@ _mapWaypointIndices: function(waypoints, instructions, coordinates) { var wps = [], wpIndices = [], - i, - idx; + i, + idx; wpIndices.push(0); wps.push(new L.Routing.Waypoint(coordinates[0], waypoints[0].name)); + for (i = 0; i < instructions.length; i++) { + if (instructions[i].type === "WaypointReached") { + idx = instructions[i].index; + 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],