Skip to content

Commit

Permalink
Merge pull request #16 from opoto/master
Browse files Browse the repository at this point in the history
Added 'response' event to propagate GraphHopper credit update
  • Loading branch information
perliedman authored May 31, 2017
2 parents 02fca7d + 41bd725 commit da02d15
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,18 @@ L.Routing.control({
```

Note that you will need to pass a valid GraphHopper API key to the constructor.

To keep track of the GraphHopper credits consumption, the application may listen to the `response` event fired by the Router object. This event holds the values from [GraphHopper's response HTTP headers](https://graphhopper.com/api/1/docs/#http-headers):
* `status`: The HTTP status code (see [GraphHopper error codes](https://graphhopper.com/api/1/docs/#http-error-codes))
* `limit`: The `X-RateLimit-Limit` header
* `remaining`: The `X-RateLimit-Remaining` header
* `reset`: The `X-RateLimit-Reset` header
* `credits`: The `X-RateLimit-Credits` header

```javascript
var router = myRoutingControl.getRouter();
router.on('response',function(e){
console.log('This routing request consumed ' + e.credits + ' credit(s)');
console.log('You have ' + e.remaining + ' left');
});
```
10 changes: 5 additions & 5 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
<head>
<meta charset="utf-8" />
<title>Leaflet OSRM Example</title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<link rel="stylesheet" href="https://rawgit.com/perliedman/leaflet-routing-machine/master/dist/leaflet-routing-machine.css" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
<link rel="stylesheet" href="http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.css" />
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div id="map" class="map"></div>
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<script src="https://rawgit.com/perliedman/leaflet-routing-machine/master/dist/leaflet-routing-machine.js"></script>
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
<script src="http://www.liedman.net/leaflet-routing-machine/dist/leaflet-routing-machine.js"></script>
<script src="../dist/lrm-graphhopper.js"></script>
<script src="Control.Geocoder.js"></script>
<script src="index.js"></script>
</body>
</html>
</html>
8 changes: 7 additions & 1 deletion examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

L.Routing.control({
var routingControl = L.Routing.control({
waypoints: [
L.latLng(57.74, 11.94),
L.latLng(57.6792, 11.949)
Expand All @@ -13,3 +13,9 @@ L.Routing.control({
router: L.Routing.graphHopper('your-api-key'),
routeWhileDragging: false
}).addTo(map);

var router = routingControl.getRouter();
router.on('response',function(e){
console.log('This request consumed ' + e.credits + ' credit(s)');
console.log('You have ' + e.remaining + ' left');
});
10 changes: 9 additions & 1 deletion src/L.Routing.GraphHopper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

L.Routing = L.Routing || {};

L.Routing.GraphHopper = L.Class.extend({
L.Routing.GraphHopper = L.Evented.extend({
options: {
serviceUrl: 'https://graphhopper.com/api/1/route',
timeout: 30 * 1000,
Expand Down Expand Up @@ -55,6 +55,14 @@

clearTimeout(timer);
if (!timedOut) {
var fired = err ? err : resp;
this.fire("response", {
status: fired.status,
limit: Number(fired.getResponseHeader("X-RateLimit-Limit")),
remaining: Number(fired.getResponseHeader("X-RateLimit-Remaining")),
reset: Number(fired.getResponseHeader("X-RateLimit-Reset")),
credits: Number(fired.getResponseHeader("X-RateLimit-Credits"))
});
if (!err) {
data = JSON.parse(resp.responseText);
this._routeDone(data, wps, callback, context);
Expand Down

0 comments on commit da02d15

Please sign in to comment.