1(function () { 2 'use strict'; 3 4 // Browserify 5 // var L = require('leaflet'); 6 // var corslite = require('corslite'); 7 // var polyline = require('polyline'); 8 9 L.Routing = L.Routing || {}; 10 11 L.Routing.OpenRouteService = L.Class.extend({ 12 options: { 13 serviceUrl: 'https://api.openrouteservice.org/v2/directions', 14 timeout: 30 * 1000, 15 urlParameters: {} 16 }, 17 18 initialize: function (apiKey, options) { 19 this._apiKey = apiKey; 20 L.Util.setOptions(this, options); 21 }, 22 23 route: function (waypoints, callback, context, options) { 24 var timedOut = false, 25 wps = [], 26 url, 27 timer, 28 wp, 29 i; 30 31 options = options || {}; 32 url = this.buildRouteUrl(waypoints, options); 33 //console.log('url', url); 34 35 timer = setTimeout(function () { 36 timedOut = true; 37 callback.call(context || callback, { 38 status: -1, 39 message: 'OpenRoueService request timed out.' 40 }); 41 }, this.options.timeout); 42 43 for (i = 0; i < waypoints.length; i++) { 44 wp = waypoints[i]; 45 wps.push({ 46 latLng: wp.latLng, 47 name: wp.name, 48 options: wp.options 49 }); 50 } 51 52 /*corslite(url, L.bind(function (err, resp) { 53 var data; 54 55 clearTimeout(timer); 56 if (!timedOut) { 57 if (!err) { 58 // try { 59 data = JSON.parse(resp.responseText); 60 this._routeDone(data, wps, callback, context); 61 } else { 62 callback.call(context || callback, { 63 status: -1, 64 message: 'HTTP request failed: ' + err 65 }); 66 } 67 } 68 }, this));*/ 69 70 let request = new XMLHttpRequest(); 71 request.open('POST', "https://api.openrouteservice.org/v2/directions/" + this.options.profile + "/geojson"); 72 request.setRequestHeader('Accept', 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8'); 73 request.setRequestHeader('Content-Type', 'application/json'); 74 request.setRequestHeader('Authorization', this._apiKey); // 75 76 request.onreadystatechange = L.bind(function (result) { 77 78 let responseText = result.target.responseText; 79 let readyState = result.target.readyState; 80 81 let data; 82 83 clearTimeout(timer); 84 if (!timedOut) { 85 if (readyState === 4) { 86 data = JSON.parse(responseText); 87 //console.log(data); 88 this._routeDone(data, wps, callback, context); 89 } 90 } 91 }, this); 92 93 94 //const body = '{"coordinates":[[8.681495,49.41461],[8.686507,49.41943],[8.687872,49.420318]],"instructions":"true","instructions_format":"text","language":"pt","preference":"recommended","units":"m"}'; 95 const body = { 96 coordinates: [[waypoints[0].latLng.lng, waypoints[0].latLng.lat], [waypoints[1].latLng.lng, waypoints[1].latLng.lat]], 97 instructions: true, 98 instructions_format: 'text', 99 //geometry_format: 'geojson', 100 preference: 'recommended', 101 units: 'm', 102 //profile: this.options.profile, 103 language: 'pt'//, 104 //extra_info: ["steepness"] 105 } 106 107 let jsonBody = JSON.stringify(body); 108 //console.log(jsonBody) 109 request.send(jsonBody); 110 111 return this; 112 }, 113 114 _routeDone: function (response, inputWaypoints, callback, context) { 115 var alts = [], 116 waypoints, 117 waypoint, 118 coordinates, 119 i, j, k, 120 instructions, 121 distance, 122 time, 123 leg, 124 steps, 125 step, 126 maneuver, 127 startingSearchIndex, 128 instruction, 129 path; 130 131 context = context || callback; 132 133 if (!response.features) { 134 callback.call(context, { 135 status: response.type, 136 message: response.details 137 }); 138 return; 139 } 140 141 for (i = 0; i < response.features.length; i++) { 142 path = response.features[i].properties; 143 coordinates = this._decodePolyline(response.features[i].geometry); 144 startingSearchIndex = 0; 145 instructions = []; 146 waypoints = []; 147 time = 0; 148 distance = 0; 149 150 for (j = 0; j < path.segments.length; j++) { 151 leg = path.segments[j]; 152 steps = leg.steps; 153 for (k = 0; k < steps.length; k++) { 154 155 step = steps[k]; 156 distance += step.distance; 157 time += step.duration;
158 instruction = this._convertInstructions(step, coordinates); 159 instructions.push(instruction); 160 waypoint = coordinates[path.way_points[1]]; 161 waypoints.push(waypoint); 162 } 163 } 164 165 let insalt = { 166 name: 'Routing option ' + i, 167 coordinates: coordinates, 168 instructions: instructions, 169 summary: { 170 totalDistance: distance, 171 totalTime: time, 172 }, 173 inputWaypoints: inputWaypoints, 174 waypoints: waypoints 175 }; 176 177 alts.push(insalt); 178 } 179 180 callback.call(context, null, alts); 181 }, 182 183 _decodePolyline: function (geometry) { 184 var polylineDefined = polyline.fromGeoJSON(geometry); 185 var coords = polyline.decode(polylineDefined, 5), 186 latlngs = new Array(coords.length), 187 i; 188 for (i = 0; i < coords.length; i++) { 189 latlngs[i] = new L.LatLng(coords[i][0], coords[i][1]); 190 } 191 192 return latlngs; 193 }, 194 195 buildRouteUrl: function (waypoints, options) { 196 var computeInstructions = 197 true, 198 locs = [], 199 i, 200 baseUrl; 201 202 for (i = 0; i < waypoints.length; i++) { 203 locs.push(waypoints[i].latLng.lng + '%2C' + waypoints[i].latLng.lat); 204 } 205 206 baseUrl = this.options.serviceUrl + '?coordinates=' + 207 locs.join('%7C'); 208 209 return baseUrl + L.Util.getParamString(L.extend({ 210 instructions: true, 211 instructions_format: 'text', 212 geometry_format: 'geojson', 213 preference: 'recommended', 214 units: 'm', 215 profile: this.options.profile, 216 language: 'pt', 217 api_key: this._apiKey 218 }, this.options.urlParameters), baseUrl); 219 }, 220 221 _convertInstructions: function (step, coordinates) { 222 return { 223 type: step.type, 224 text: step.instruction, 225 distance: step.distance, 226 time: step.duration, 227 index: step.way_points[0] 228 }; 229 }, 230 }); 231 232 L.Routing.openrouteservice = function (apiKey, options) { 233 return new L.Routing.OpenRouteService(apiKey, options); 234 }; 235 236 // Browserify 237 // module.exports = L.Routing.OpenRouteService; 238})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.