Giter VIP home page Giter VIP logo

directions-api-js-client's Introduction

JavaScript client for the Directions API

This project offers JavaScript clients for the GraphHopper Directions API.

Getting Started

NPM

Install the lib with npm:

npm install graphhopper-js-api-client --save

You can either require the whole client enabling you to use every GraphHopper API, but you can also only require the pieces you need.

require('graphhopper-js-api-client');
 
window.onload = function() {
  let defaultKey = "[Sign-up for free and get your own key: https://www.graphhopper.com/products/]";
  let ghRouting = new GraphHopper.Routing({key: defaultKey}, {profile:"car", elevation: false});

  ghRouting.doRequest({points:[[8.534317, 47.400905], [8.538265, 47.394108]]})
    .then(json => {
       // Add your own result handling here
       console.log(json);
    })
    .catch(err => {
       console.error(err.message);
    });
};

Running Tests

In order to run the tests, you have to register for a key on GraphHopper. Either set your key as environment variable using export GHKEY=YOUR_KEY or set your key in spec/helpers/config.js. You can run all tests via npm test. If you only want to run a single spec file, you can use the --spec option, e.g., npm test --spec spec/GraphHopperRoutingSpec.js.

Integrate the APIs in your application

You can either use our bundled version, including all APIs or you can use only the pieces you need.

GraphHopper Routing API

GraphHopper Routing API screenshot

You need the routing client.

There is also a different client developed from the community here.

GraphHopper Route Optimization API

Route Optimization API screenshot

You need the optimization client.

GraphHopper Isochrone API

GraphHopper Isochrone API screenshot

You need the isochrone client

GraphHopper Matrix API

GraphHopper Matrix API screenshot

You need the matrix client.

GraphHopper Geocoding API

GraphHopper Geocoding API screenshot

You need the geocoding client.

GraphHopper Map Matching API

GraphHopper Map Matching API screenshot

You need the map matching client and the togeojson.js

Releasing a new Version to NPM

Set the version you like to publish in the package.json. Every version can only be published once and cannot be overwritten.

Tag the commit you like to publish for example like this:

git log # get the commit hash of the commit you want to tag
git tag <tag> <commit-hash>
git push origin --tag <tag>

GitHub will then build and publish the commit to NPM.

License

Code stands under Apache License 2.0

directions-api-js-client's People

Contributors

alainfolletete avatar alilia avatar boldtrn avatar dependabot[bot] avatar karussell avatar michaz avatar oblonski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

directions-api-js-client's Issues

make it possible to add multiple block_area

Current workaround is to hardcore the semicolon and keep all the parameters in one array as strings. E.g (for areas in the form of lat, lng, radius)

block_area = [[1,1,1],[3,4,5],...]           doesn’t work
block_area = [ "1", "1", "1”, ";3", ”4”, "5"...]        does

Host @CDN

I think it would be a nice improvement to host the js-client at a CDN. An easy solution seems to be this CDN. Another example would be cdnjs.

Better debugging

Its a bit hard to work with the npm package because when doRequest does not work it seems that the only return error message is that there is a bad request.

I was able to make a request work. Then I changed the address data for the services of the optimized route and all I get was "Bad Request".

After checking my code I saw that the lat/lng were strings instead of floats. After changing it it worked fine.

It would be nice if the returned error would give some hints what the problem could be.

How can I use this library in Angular?

Hello,

By the guideline, I did like below:

  1. Install
    npm install graphhopper-js-api-client --save

  2. Import libs I need only
    import * as GraphHopperRouting from 'graphhopper-js-api-client/src/GraphHopperRouting';
    import * as GHInput from 'graphhopper-js-api-client/src/GHInput';

  3. Call functions
    var ghRouting = new GraphHopperRouting({key: 'defaultKey', host: 'host', vehicle: 'car', elevation: false});

    // Setup your own Points
    ghRouting.addPoint(new GHInput(47.400905, 8.534317));
    ghRouting.addPoint(new GHInput(47.394108, 8.538265));

  4. Error comes out
    Uncaught ReferenceError: GraphHopperRouting is not defined at Object../node_modules/graphhopper-js-api-client/src/GraphHopperRouting.js (GraphHopperRouting.js:7) at __webpack_require__ (bootstrap:84)

How to use Route Optimization API?

Hi, I've installed the js client and I would like to use Route Optimization API, but I'm not sure how I can use it.
I've required the whole client using require('graphhopper-js-api-client') and tried to call Routing with the parameter optimize:true as explained here :

var ghRouting = new GraphHopper.Routing({key: defaultKey, host: host, vehicle:profile,elevation:false,optimize:true})

The response I get are the points not sorted, they are in the order I've added them.
I've also tried using
var ghRouting = new GraphHopper.Optimization({key: defaultKey, host: host, vehicle_types:profile,elevation:false,calc_points:true})

But doesn't work. In the docs for Route Optimization API is not explained how to specifiy parameters for the constructor, so I'm a bit stuck.

Graphhopper run time error “GraphHopperRouting is not a constructor”

I wanted to use graphhopper js client in my Ionic app, so following the instructions explained here I have installed the package using npm, then tried to use the routing API.
All I did was import this:

import {graphhopper, GraphHopperRouting, GHInput} from 'graphhopper-js-api-client';

And then wrote:

var defaultKey = "[key]";
var profile = "foot";
var host;
var ghRouting = new GraphHopperRouting({key: defaultKey, host: host, vehicle:profile,elevation:false})

ghRouting.addPoint(new GHInput(47.400905, 8.534317));
 ghRouting.addPoint(new GHInput(47.394108, 8.538265));

 ghRouting.doRequest().then(function(json){
   console.log("Result: " + json)
 }).catch(function(err){
   console.log(err.message)
 })

Exactly as the guide explained.
When I run the code, the following error is shown:

Runtime Error
 __WEBPACK_IMPORTED_MODULE_6_graphhopper_js_api_client__.GraphHopperRouting is not a constructor

Debugging error handling

I'm currently trying to update a project using the old client (raw JS files) and have utilized graphhopper-client.js but I'm getting:

Unhandled rejection [11]</GHUtil.prototype.extractError@http://localhost:9000/js/graphhopper-client.js:33:792
[16]</GraphHopperOptimization.prototype.doRawRequest/</<@http://localhost:9000/js/graphhopper-client.js:47:1505
[4]</Request.prototype.callback@http://localhost:9000/js/graphhopper-client.js:15:5622
Request/<@http://localhost:9000/js/graphhopper-client.js:15:2198
[2]</Emitter.prototype.emit@http://localhost:9000/js/graphhopper-client.js:9:1093
[4]</Request.prototype._end/t.onreadystatechange@http://localhost:9000/js/graphhopper-client.js:15:6965

I think here is something wrong in the client but could also be in my usage. How do I create and associate a map file for debugging?

Related graphhopper/directions-api-clients#13

An error occured: Bad Request

An error occured: Bad Request - for url https://graphhopper.com/api/1/vrp/optimize?key=xxxxx

Hi

i create a local instance for testing localhost/graphhopper
i use the examples document for create my testing code i only modify the following code in examples, i put my key and try the demo localhost, working good with all items except when i change the coords lat and lon in the file tsp_lonlan_new.json
modify demo.js for set the center map

    $("#set_example_tsp").click(function () {
        $.getJSON("route-optimization-examples/tsp_lonlat_new.json?v=" + exampleVersion, function (jsonData) {

            clearMap();
            map.setView([4, -74], 6);
            $("#vrp-response").text("Calculating ...");
            ghOptimization.doRequest(jsonData)
                .then(optimizeResponse)
                .catch(optimizeError);
        });
    });

modify route-optimization-examples/tsp_lonlat_new.json for points

{
  "vehicles" : [{
    "vehicle_id" : "vehicle1",
    "start_address" : {
        "location_id" : "v1_startLoc",
        "lon": 4.713042,
        "lat": -74.05115
    },
    "type_id" : "vehicle_type_1"
  }],
  "vehicle_types" : [{
    "type_id" : "vehicle_type_1",
    "profile" : "car"
  }],
  "services" : [
    {
      "id": "b1",
      "name": "drink_bionade_in_rostock",
      "address": {
        "location_id": "loc_b1",
        "lon": 4.663407,
        "lat": -74.049069
      }
    },
    {
      "id": "b2",
      "name": "drink_cola_in_berlin",
      "address": {
        "location_id": "loc_b2",
        "lon": 4.744023,
        "lat": -74.064401
      }
    }
  ]
}

thanks for any help

Improve Map Matching usage

  • code examples for MapMatching (workaround: use live examples page)
  • instead of generating a GPX string and passing that into the ghMapMatching.doRequest(gpxString) we could try to make it more like the Routing JS client (using ghRouting.addPoint)

Add Coordinate to PathDetails

We should resolve the indices of the PathDetails with actual coordinates similar to what we are doing with the instructions. This will make it easier for consumers to use the PahtDetails.

Make Client more flexible

An issue with the client right now is that if we add new parameters to the API, we have to change the client as well.

It would be great if we could make the client a bit more flexible. We could dynamically convert the args object into a request string and only add special handling for a few parameters or maybe we add a second route method that accepts a URL so that users can write their own url conversion if needed.

Url seems to be build wrong with doRequest

Hey!

I am a bit confused. I am pretty sure I have done similar requests before but today I got the error that I have to specify at least 2 points.

My request object:

// Request Object

{
   "vehicles":[
      {
         "vehicle_id":"vehicle1",
         "start_address":{
            "location_id":"r8SvLjPsCxedseLBS",
            "lon":8.4573,
            "lat":47.18206
         },
         "type_id":"vehicle_type_1"
      }
   ],
   "vehicle_types":[
      {
         "type_id":"vehicle_type_1",
         "profile":"car"
      }
   ],
   "services":[
      {
         "id":"3MoDrkrjRXdFgKQ6S",
         "name":"Born Hansruedi_Niederbipp_Hansruedi Born_3.71ha_20WP",
         "address":{
            "location_id":"field-3MoDrkrjRXdFgKQ6S",
            "lon":7.71862,
            "lat":47.25543
         }
      },
      {
         "id":"WQ3Nv4KtPBNHvZGR7",
         "name":"Born Hansruedi_Niederbipp_Hansruedi Born_1.41ha_6WP",
         "address":{
            "location_id":"field-WQ3Nv4KtPBNHvZGR7",
            "lon":7.71651,
            "lat":47.26037
         }
      },
      {
         "id":"xMKGdQ3S4aXft8nk3",
         "name":"Feld 1_aesch_regina nu_3.92ha_0WP",
         "address":{
            "location_id":"field-xMKGdQ3S4aXft8nk3",
            "lon":47.48199,
            "lat":7.57499
         }
      }
   ]
}

After init the gh routing class in nodejs I am doing:

import GraphHopperOptimization from 'graphhopper-js-api-client/src/GraphHopperOptimization';

const ghRouting = new GraphHopperOptimization({ key: '..', ...});

ghRouting.doRequest(requestObject).then(() => { ... }).catch((e) => { .... })

this will return the error I wrote above. As far as I can see I have 3 points (if the errors references to the list of services) I am confused. Am I missing here something?

For better understanding this is the full error msg:

Specify at least 2 points - for url https://graphhopper.com/api/1/route?&vehicle=car&debug=false&locale=en&points_encoded=true&instructions=true&elevation=false&optimize=false&key=$$$KEY$$&vehicles.vehicle_id=vehicle1&vehicles.start_address.location_id=r8SvLjPsCxedseLBS&vehicles.start_address.lon=8.4573&vehicles.start_address.lat=47.18206&vehicles.type_id=vehicle_type_1&vehicle_types.type_id=vehicle_type_1&vehicle_types.profile=car&services.id=3MoDrkrjRXdFgKQ6S&services.name=Born%20Hansruedi_Niederbipp_Hansruedi%20Born_3.71ha_20WP&services.address.location_id=field-3MoDrkrjRXdFgKQ6S&services.address.lon=7.71862&services.address.lat=47.25543&services.id=WQ3Nv4KtPBNHvZGR7&services.name=Born%20Hansruedi_Niederbipp_Hansruedi%20Born_1.41ha_6WP&services.address.location_id=field-WQ3Nv4KtPBNHvZGR7&services.address.lon=7.71651&services.address.lat=47.26037&services.id=xMKGdQ3S4aXft8nk3&services.name=Feld%201_aesch_regina%20nu_3.92ha_0WP&services.address.location_id=field-xMKGdQ3S4aXft8nk3&services.address.lon=47.48199&services.address.lat=7.57499

It seems that services is not added as an array. instead the generated url is overwriting the services before so at the end there is only one services.name, services.address, ....

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.