Using GraphHopper new API
This commit is contained in:
parent
9126653ffd
commit
873c1cb84e
1 changed files with 23 additions and 16 deletions
|
@ -15,9 +15,13 @@ GraphHopperEngine.prototype.createConfig = function() {
|
||||||
draggable: false,
|
draggable: false,
|
||||||
_hints: {},
|
_hints: {},
|
||||||
getRoute: function(isFinal, points) {
|
getRoute: function(isFinal, points) {
|
||||||
var url = "http://graphhopper.com/routing/api/route?"
|
// documentation
|
||||||
|
// https://github.com/graphhopper/graphhopper/blob/master/docs/web/api-doc.md
|
||||||
|
var url = "http://graphhopper.com/api/1/route?"
|
||||||
+ that.vehicleParam
|
+ that.vehicleParam
|
||||||
+ "&locale=" + I18n.currentLocale();
|
+ "&locale=" + I18n.currentLocale()
|
||||||
|
+ "&key=LijBPDQGfu7Iiq80w3HzwB4RUDJbMbhs6BU0dEnn";
|
||||||
|
|
||||||
for (var i = 0; i < points.length; i++) {
|
for (var i = 0; i < points.length; i++) {
|
||||||
var pair = points[i].join(',');
|
var pair = points[i].join(',');
|
||||||
url += "&point=" + pair;
|
url += "&point=" + pair;
|
||||||
|
@ -28,25 +32,26 @@ GraphHopperEngine.prototype.createConfig = function() {
|
||||||
this.requestJSONP(url + "&type=jsonp&callback=");
|
this.requestJSONP(url + "&type=jsonp&callback=");
|
||||||
},
|
},
|
||||||
gotRoute: function(router, data) {
|
gotRoute: function(router, data) {
|
||||||
if (!data.info.routeFound) {
|
if (!data.paths || data.paths.length == 0)
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
// Draw polyline
|
// Draw polyline
|
||||||
var line = L.PolylineUtil.decode(data.route.coordinates);
|
var path = data.paths[0];
|
||||||
|
var line = L.PolylineUtil.decode(path.points);
|
||||||
router.setPolyline(line);
|
router.setPolyline(line);
|
||||||
// Assemble instructions
|
// Assemble instructions
|
||||||
var steps = [];
|
var steps = [];
|
||||||
var instr = data.route.instructions;
|
var len = path.instructions.length;
|
||||||
for (i = 0; i < instr.descriptions.length; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
var indi = instr.indications[i];
|
var instr = path.instructions[i];
|
||||||
var instrCode = (i == instr.descriptions.length - 1) ? 15 : this.GH_INSTR_MAP[indi];
|
var instrCode = (i === len - 1) ? 15 : this.GH_INSTR_MAP[instr.sign];
|
||||||
var instrText = "<b>" + (i + 1) + ".</b> ";
|
var instrText = "<b>" + (i + 1) + ".</b> ";
|
||||||
instrText += instr.descriptions[i];
|
instrText += instr.text;
|
||||||
var latlng = instr.latLngs[i];
|
var latLng = line[instr.interval[0]];
|
||||||
var distInMeter = instr.distances[i];
|
var distInMeter = instr.distance;
|
||||||
steps.push([{lat: latlng[0], lng: latlng[1]}, instrCode, instrText, distInMeter, []]); // TODO does graphhopper map instructions onto line indices?
|
steps.push([{lat: latLng.lat, lng: latLng.lng}, instrCode, instrText, distInMeter, []]); // TODO does graphhopper map instructions onto line indices?
|
||||||
}
|
}
|
||||||
router.setItinerary({ steps: steps, distance: data.route.distance, time: data.route['time']/1000 });
|
router.setItinerary({ steps: steps, distance: path.distance, time: path.time / 1000 });
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
GH_INSTR_MAP: {
|
GH_INSTR_MAP: {
|
||||||
|
@ -56,7 +61,9 @@ GraphHopperEngine.prototype.createConfig = function() {
|
||||||
0: 0, // straight
|
0: 0, // straight
|
||||||
1: 1, // slight right
|
1: 1, // slight right
|
||||||
2: 2, // right
|
2: 2, // right
|
||||||
3: 3 // sharp right
|
3: 3, // sharp right
|
||||||
|
4: -1, // finish reached
|
||||||
|
5: -1 // via reached
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue