Skip to content

Commit

Permalink
not beautiful, but working :bowtie:
Browse files Browse the repository at this point in the history
  • Loading branch information
mrohnstock committed Mar 25, 2015
1 parent c2bdd1f commit 1ab159e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 58 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
29 changes: 0 additions & 29 deletions scripts/publish.sh

This file was deleted.

92 changes: 64 additions & 28 deletions src/L.Routing.TomTom.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,47 +70,51 @@

_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;
}

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);
},

_decodePolyline: function(geometry) {
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);
}
Expand All @@ -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]),
Expand All @@ -142,27 +146,59 @@

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;
},

_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],
Expand Down

0 comments on commit 1ab159e

Please sign in to comment.