Skip to content

Commit bd0407a

Browse files
committed
3.0.0
1 parent 4dc4844 commit bd0407a

File tree

3 files changed

+150
-123
lines changed

3 files changed

+150
-123
lines changed

dist/leaflet-routing-machine.js

+146-119
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),(f.L||(f.L={})).Routing=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
1+
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}(g.L || (g.L = {})).Routing = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
22
function corslite(url, callback, cors) {
33
var sent = false;
44

@@ -401,7 +401,7 @@ if (typeof module !== undefined) module.exports = polyline;
401401
L.extend(L.Routing, require('./L.Routing.Itinerary'));
402402
L.extend(L.Routing, require('./L.Routing.Line'));
403403
L.extend(L.Routing, require('./L.Routing.Plan'));
404-
L.extend(L.Routing, require('./L.Routing.OSRM'));
404+
L.extend(L.Routing, require('./L.Routing.OSRMv1'));
405405
L.extend(L.Routing, require('./L.Routing.ErrorControl'));
406406

407407
L.Routing.Control = L.Routing.Itinerary.extend({
@@ -419,7 +419,7 @@ if (typeof module !== undefined) module.exports = polyline;
419419
initialize: function(options) {
420420
L.Util.setOptions(this, options);
421421

422-
this._router = this.options.router || new L.Routing.OSRM(options);
422+
this._router = this.options.router || new L.Routing.OSRMv1(options);
423423
this._plan = this.options.plan || L.Routing.plan(this.options.waypoints, options);
424424
this._requestCount = 0;
425425

@@ -705,7 +705,7 @@ if (typeof module !== undefined) module.exports = polyline;
705705
})();
706706

707707
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
708-
},{"./L.Routing.ErrorControl":5,"./L.Routing.Itinerary":8,"./L.Routing.Line":10,"./L.Routing.OSRM":12,"./L.Routing.Plan":13}],5:[function(require,module,exports){
708+
},{"./L.Routing.ErrorControl":5,"./L.Routing.Itinerary":8,"./L.Routing.Line":10,"./L.Routing.OSRMv1":12,"./L.Routing.Plan":13}],5:[function(require,module,exports){
709709
(function() {
710710
'use strict';
711711

@@ -2016,12 +2016,20 @@ if (typeof module !== undefined) module.exports = polyline;
20162016
L.Routing = L.Routing || {};
20172017
L.extend(L.Routing, require('./L.Routing.Waypoint'));
20182018

2019-
L.Routing.OSRM = L.Class.extend({
2019+
/**
2020+
* Works against OSRM's new API in version 5.0; this has
2021+
* the API version v1.
2022+
*/
2023+
L.Routing.OSRMv1 = L.Class.extend({
20202024
options: {
2021-
serviceUrl: 'https://router.project-osrm.org/viaroute',
2025+
serviceUrl: 'http://router.project-osrm.org/route/v1',
2026+
profile: 'driving',
20222027
timeout: 30 * 1000,
2023-
routingOptions: {},
2024-
polylinePrecision: 6
2028+
routingOptions: {
2029+
alternatives: true,
2030+
steps: true
2031+
},
2032+
polylinePrecision: 5
20252033
},
20262034

20272035
initialize: function(options) {
@@ -2071,7 +2079,7 @@ if (typeof module !== undefined) module.exports = polyline;
20712079
try {
20722080
data = JSON.parse(resp.responseText);
20732081
try {
2074-
return this._routeDone(data, wps, callback, context);
2082+
return this._routeDone(data, wps, options, callback, context);
20752083
} catch (ex) {
20762084
statusCode = -3;
20772085
errorMessage = ex.toString();
@@ -2092,58 +2100,130 @@ if (typeof module !== undefined) module.exports = polyline;
20922100
return this;
20932101
},
20942102

2095-
_routeDone: function(response, inputWaypoints, callback, context) {
2096-
var coordinates,
2097-
alts,
2103+
_routeDone: function(response, inputWaypoints, options, callback, context) {
2104+
var alts = [],
20982105
actualWaypoints,
2099-
i;
2106+
i,
2107+
route;
21002108

21012109
context = context || callback;
2102-
if (response.status !== 0 && response.status !== 200) {
2110+
if (response.code !== 'Ok') {
21032111
callback.call(context, {
2104-
status: response.status,
2105-
message: response.status_message
2112+
status: response.code
21062113
});
21072114
return;
21082115
}
21092116

2110-
coordinates = this._decodePolyline(response.route_geometry);
2111-
actualWaypoints = this._toWaypoints(inputWaypoints, response.via_points);
2112-
alts = [{
2113-
name: this._createName(response.route_name),
2114-
coordinates: coordinates,
2115-
instructions: response.route_instructions ? this._convertInstructions(response.route_instructions) : [],
2116-
summary: response.route_summary ? this._convertSummary(response.route_summary) : [],
2117-
inputWaypoints: inputWaypoints,
2118-
waypoints: actualWaypoints,
2119-
waypointIndices: this._clampIndices(response.via_indices, coordinates)
2120-
}];
2121-
2122-
if (response.alternative_geometries) {
2123-
for (i = 0; i < response.alternative_geometries.length; i++) {
2124-
coordinates = this._decodePolyline(response.alternative_geometries[i]);
2125-
alts.push({
2126-
name: this._createName(response.alternative_names[i]),
2127-
coordinates: coordinates,
2128-
instructions: response.alternative_instructions[i] ? this._convertInstructions(response.alternative_instructions[i]) : [],
2129-
summary: response.alternative_summaries[i] ? this._convertSummary(response.alternative_summaries[i]) : [],
2130-
inputWaypoints: inputWaypoints,
2131-
waypoints: actualWaypoints,
2132-
waypointIndices: this._clampIndices(response.alternative_geometries.length === 1 ?
2133-
// Unsure if this is a bug in OSRM or not, but alternative_indices
2134-
// does not appear to be an array of arrays, at least not when there is
2135-
// a single alternative route.
2136-
response.alternative_indices : response.alternative_indices[i],
2137-
coordinates)
2138-
});
2117+
actualWaypoints = this._toWaypoints(inputWaypoints, response.waypoints);
2118+
2119+
for (i = 0; i < response.routes.length; i++) {
2120+
route = this._convertRoute(response.routes[i], options.geometryOnly);
2121+
route.inputWaypoints = inputWaypoints;
2122+
route.waypoints = actualWaypoints;
2123+
alts.push(route);
2124+
}
2125+
2126+
this._saveHintData(response.waypoints, inputWaypoints);
2127+
2128+
callback.call(context, null, alts);
2129+
},
2130+
2131+
_convertRoute: function(responseRoute, geometryOnly) {
2132+
var result = {
2133+
name: '', // TODO
2134+
summary: {
2135+
totalDistance: responseRoute.distance,
2136+
totalTime: responseRoute.duration
2137+
}
2138+
},
2139+
coordinates = [],
2140+
instructions = [],
2141+
index = 0,
2142+
legCount = responseRoute.legs.length,
2143+
i,
2144+
j,
2145+
leg,
2146+
step,
2147+
geometry,
2148+
type;
2149+
2150+
if (geometryOnly) {
2151+
result.coordinates = this._decodePolyline(responseRoute.geometry);
2152+
return result;
2153+
}
2154+
2155+
for (i = 0; i < legCount; i++) {
2156+
leg = responseRoute.legs[i];
2157+
for (j = 0; j < leg.steps.length; j++) {
2158+
step = leg.steps[j];
2159+
geometry = this._decodePolyline(step.geometry);
2160+
coordinates.push(geometry);
2161+
type = this._maneuverToInstructionType(step.maneuver, i === legCount - 1);
2162+
if (type) {
2163+
instructions.push({
2164+
type: type,
2165+
distance: step.distance,
2166+
time: step.duration,
2167+
road: step.name,
2168+
direction: this._bearingToDirection(step.maneuver.bearing_after),
2169+
exit: step.maneuver.exit,
2170+
index: index
2171+
});
2172+
}
2173+
2174+
index += geometry.length;
21392175
}
21402176
}
21412177

2142-
// only versions <4.5.0 will support this flag
2143-
if (response.hint_data) {
2144-
this._saveHintData(response.hint_data, inputWaypoints);
2178+
result.coordinates = Array.prototype.concat.apply([], coordinates);
2179+
result.instructions = instructions;
2180+
2181+
return result;
2182+
},
2183+
2184+
_bearingToDirection: function(bearing) {
2185+
var oct = Math.round(bearing / 45) % 8;
2186+
return ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'][oct];
2187+
},
2188+
2189+
_maneuverToInstructionType: function(maneuver, lastLeg) {
2190+
switch (maneuver.type) {
2191+
case 'turn':
2192+
case 'end of road':
2193+
switch (maneuver.modifier) {
2194+
case 'straight':
2195+
return 'Straight';
2196+
case 'slight right':
2197+
return 'SlightRight';
2198+
case 'right':
2199+
return 'Right';
2200+
case 'sharp right':
2201+
return 'SharpRight';
2202+
case 'sharp left':
2203+
return 'SharpLeft';
2204+
case 'left':
2205+
return 'Left';
2206+
case 'slight left':
2207+
return 'SlightLeft';
2208+
case 'uturn':
2209+
return 'TurnAround';
2210+
default:
2211+
return null;
2212+
}
2213+
break;
2214+
case 'new name':
2215+
case 'merge':
2216+
case 'ramp':
2217+
case 'fork':
2218+
return 'Continue';
2219+
case 'arrive':
2220+
return lastLeg ? 'DestinationReached' : 'WaypointReached';
2221+
case 'roundabout':
2222+
case 'rotary':
2223+
return 'Roundabout';
2224+
default:
2225+
return null;
21452226
}
2146-
callback.call(context, null, alts);
21472227
},
21482228

21492229
_decodePolyline: function(routeGeometry) {
@@ -2187,53 +2267,43 @@ if (typeof module !== undefined) module.exports = polyline;
21872267

21882268
buildRouteUrl: function(waypoints, options) {
21892269
var locs = [],
2270+
hints = [],
21902271
wp,
2272+
latLng,
21912273
computeInstructions,
2192-
computeAlternative,
2193-
locationKey,
2194-
hint;
2274+
computeAlternative;
21952275

21962276
for (var i = 0; i < waypoints.length; i++) {
21972277
wp = waypoints[i];
2198-
locationKey = this._locationKey(wp.latLng);
2199-
locs.push('loc=' + locationKey);
2200-
2201-
hint = this._hints.locations[locationKey];
2202-
if (hint) {
2203-
locs.push('hint=' + hint);
2204-
}
2205-
2206-
if (wp.options && wp.options.allowUTurn) {
2207-
locs.push('u=true');
2208-
}
2278+
latLng = wp.latLng;
2279+
locs.push(latLng.lng + ',' + latLng.lat);
2280+
hints.push(this._hints[this._locationKey(latLng)] || '');
22092281
}
22102282

22112283
computeAlternative = computeInstructions =
22122284
!(options && options.geometryOnly);
22132285

2214-
return this.options.serviceUrl + '?' +
2215-
'instructions=' + computeInstructions.toString() + '&' +
2216-
'alt=' + computeAlternative.toString() + '&' +
2217-
(options.z ? 'z=' + options.z + '&' : '') +
2218-
locs.join('&') +
2219-
(this._hints.checksum !== undefined ? '&checksum=' + this._hints.checksum : '') +
2220-
(options.fileformat ? '&output=' + options.fileformat : '') +
2221-
(options.allowUTurns ? '&uturns=' + options.allowUTurns : '');
2286+
return this.options.serviceUrl + '/' + this.options.profile + '/' +
2287+
locs.join(';') + '?' +
2288+
(options.z ? 'z=' + options.z + '&' : (options.geometryOnly ? 'overview=full&' : '')) +
2289+
//'hints=' + hints.join(';') + '&' +
2290+
'alternatives=' + computeAlternative.toString() + '&' +
2291+
'steps=' + computeInstructions.toString() +
2292+
(options.allowUTurns ? '&continue_straight=' + !options.allowUTurns : '');
22222293
},
22232294

22242295
_locationKey: function(location) {
22252296
return location.lat + ',' + location.lng;
22262297
},
22272298

2228-
_saveHintData: function(hintData, waypoints) {
2299+
_saveHintData: function(actualWaypoints, waypoints) {
22292300
var loc;
22302301
this._hints = {
2231-
checksum: hintData.checksum,
22322302
locations: {}
22332303
};
2234-
for (var i = hintData.locations.length - 1; i >= 0; i--) {
2304+
for (var i = actualWaypoints.length - 1; i >= 0; i--) {
22352305
loc = waypoints[i].latLng;
2236-
this._hints.locations[this._locationKey(loc)] = hintData.locations[i];
2306+
this._hints.locations[this._locationKey(loc)] = actualWaypoints[i].hint;
22372307
}
22382308
},
22392309

@@ -2270,52 +2340,9 @@ if (typeof module !== undefined) module.exports = polyline;
22702340

22712341
return result;
22722342
},
2273-
2274-
_drivingDirectionType: function(d) {
2275-
switch (parseInt(d, 10)) {
2276-
case 1:
2277-
return 'Straight';
2278-
case 2:
2279-
return 'SlightRight';
2280-
case 3:
2281-
return 'Right';
2282-
case 4:
2283-
return 'SharpRight';
2284-
case 5:
2285-
return 'TurnAround';
2286-
case 6:
2287-
return 'SharpLeft';
2288-
case 7:
2289-
return 'Left';
2290-
case 8:
2291-
return 'SlightLeft';
2292-
case 9:
2293-
return 'WaypointReached';
2294-
case 10:
2295-
// TODO: "Head on"
2296-
// https://github.com/DennisOSRM/Project-OSRM/blob/master/DataStructures/TurnInstructions.h#L48
2297-
return 'Straight';
2298-
case 11:
2299-
case 12:
2300-
return 'Roundabout';
2301-
case 15:
2302-
return 'DestinationReached';
2303-
default:
2304-
return null;
2305-
}
2306-
},
2307-
2308-
_clampIndices: function(indices, coords) {
2309-
var maxCoordIndex = coords.length - 1,
2310-
i;
2311-
for (i = 0; i < indices.length; i++) {
2312-
indices[i] = Math.min(maxCoordIndex, Math.max(indices[i], 0));
2313-
}
2314-
return indices;
2315-
}
23162343
});
23172344

2318-
L.Routing.osrm = function(options) {
2345+
L.Routing.osrmv1 = function(options) {
23192346
return new L.Routing.OSRM(options);
23202347
};
23212348

0 commit comments

Comments
 (0)