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 ) {
2
2
function corslite ( url , callback , cors ) {
3
3
var sent = false ;
4
4
@@ -401,7 +401,7 @@ if (typeof module !== undefined) module.exports = polyline;
401
401
L . extend ( L . Routing , require ( './L.Routing.Itinerary' ) ) ;
402
402
L . extend ( L . Routing , require ( './L.Routing.Line' ) ) ;
403
403
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 ' ) ) ;
405
405
L . extend ( L . Routing , require ( './L.Routing.ErrorControl' ) ) ;
406
406
407
407
L . Routing . Control = L . Routing . Itinerary . extend ( {
@@ -419,7 +419,7 @@ if (typeof module !== undefined) module.exports = polyline;
419
419
initialize : function ( options ) {
420
420
L . Util . setOptions ( this , options ) ;
421
421
422
- this . _router = this . options . router || new L . Routing . OSRM ( options ) ;
422
+ this . _router = this . options . router || new L . Routing . OSRMv1 ( options ) ;
423
423
this . _plan = this . options . plan || L . Routing . plan ( this . options . waypoints , options ) ;
424
424
this . _requestCount = 0 ;
425
425
@@ -705,7 +705,7 @@ if (typeof module !== undefined) module.exports = polyline;
705
705
} ) ( ) ;
706
706
707
707
} ) . 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 ) {
709
709
( function ( ) {
710
710
'use strict' ;
711
711
@@ -2016,12 +2016,20 @@ if (typeof module !== undefined) module.exports = polyline;
2016
2016
L . Routing = L . Routing || { } ;
2017
2017
L . extend ( L . Routing , require ( './L.Routing.Waypoint' ) ) ;
2018
2018
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 ( {
2020
2024
options : {
2021
- serviceUrl : 'https://router.project-osrm.org/viaroute' ,
2025
+ serviceUrl : 'http://router.project-osrm.org/route/v1' ,
2026
+ profile : 'driving' ,
2022
2027
timeout : 30 * 1000 ,
2023
- routingOptions : { } ,
2024
- polylinePrecision : 6
2028
+ routingOptions : {
2029
+ alternatives : true ,
2030
+ steps : true
2031
+ } ,
2032
+ polylinePrecision : 5
2025
2033
} ,
2026
2034
2027
2035
initialize : function ( options ) {
@@ -2071,7 +2079,7 @@ if (typeof module !== undefined) module.exports = polyline;
2071
2079
try {
2072
2080
data = JSON . parse ( resp . responseText ) ;
2073
2081
try {
2074
- return this . _routeDone ( data , wps , callback , context ) ;
2082
+ return this . _routeDone ( data , wps , options , callback , context ) ;
2075
2083
} catch ( ex ) {
2076
2084
statusCode = - 3 ;
2077
2085
errorMessage = ex . toString ( ) ;
@@ -2092,58 +2100,130 @@ if (typeof module !== undefined) module.exports = polyline;
2092
2100
return this ;
2093
2101
} ,
2094
2102
2095
- _routeDone : function ( response , inputWaypoints , callback , context ) {
2096
- var coordinates ,
2097
- alts ,
2103
+ _routeDone : function ( response , inputWaypoints , options , callback , context ) {
2104
+ var alts = [ ] ,
2098
2105
actualWaypoints ,
2099
- i ;
2106
+ i ,
2107
+ route ;
2100
2108
2101
2109
context = context || callback ;
2102
- if ( response . status !== 0 && response . status !== 200 ) {
2110
+ if ( response . code !== 'Ok' ) {
2103
2111
callback . call ( context , {
2104
- status : response . status ,
2105
- message : response . status_message
2112
+ status : response . code
2106
2113
} ) ;
2107
2114
return ;
2108
2115
}
2109
2116
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 ;
2139
2175
}
2140
2176
}
2141
2177
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 ;
2145
2226
}
2146
- callback . call ( context , null , alts ) ;
2147
2227
} ,
2148
2228
2149
2229
_decodePolyline : function ( routeGeometry ) {
@@ -2187,53 +2267,43 @@ if (typeof module !== undefined) module.exports = polyline;
2187
2267
2188
2268
buildRouteUrl : function ( waypoints , options ) {
2189
2269
var locs = [ ] ,
2270
+ hints = [ ] ,
2190
2271
wp ,
2272
+ latLng ,
2191
2273
computeInstructions ,
2192
- computeAlternative ,
2193
- locationKey ,
2194
- hint ;
2274
+ computeAlternative ;
2195
2275
2196
2276
for ( var i = 0 ; i < waypoints . length ; i ++ ) {
2197
2277
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 ) ] || '' ) ;
2209
2281
}
2210
2282
2211
2283
computeAlternative = computeInstructions =
2212
2284
! ( options && options . geometryOnly ) ;
2213
2285
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 : '' ) ;
2222
2293
} ,
2223
2294
2224
2295
_locationKey : function ( location ) {
2225
2296
return location . lat + ',' + location . lng ;
2226
2297
} ,
2227
2298
2228
- _saveHintData : function ( hintData , waypoints ) {
2299
+ _saveHintData : function ( actualWaypoints , waypoints ) {
2229
2300
var loc ;
2230
2301
this . _hints = {
2231
- checksum : hintData . checksum ,
2232
2302
locations : { }
2233
2303
} ;
2234
- for ( var i = hintData . locations . length - 1 ; i >= 0 ; i -- ) {
2304
+ for ( var i = actualWaypoints . length - 1 ; i >= 0 ; i -- ) {
2235
2305
loc = waypoints [ i ] . latLng ;
2236
- this . _hints . locations [ this . _locationKey ( loc ) ] = hintData . locations [ i ] ;
2306
+ this . _hints . locations [ this . _locationKey ( loc ) ] = actualWaypoints [ i ] . hint ;
2237
2307
}
2238
2308
} ,
2239
2309
@@ -2270,52 +2340,9 @@ if (typeof module !== undefined) module.exports = polyline;
2270
2340
2271
2341
return result ;
2272
2342
} ,
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
- }
2316
2343
} ) ;
2317
2344
2318
- L . Routing . osrm = function ( options ) {
2345
+ L . Routing . osrmv1 = function ( options ) {
2319
2346
return new L . Routing . OSRM ( options ) ;
2320
2347
} ;
2321
2348
0 commit comments