Skip to content

Latest commit

 

History

History
56 lines (38 loc) · 2.77 KB

routing.md

File metadata and controls

56 lines (38 loc) · 2.77 KB

Routing via Java API

To use the following examples you need to specify the dependency in your Maven config correctly.

To do routing in your Java code you'll need just a few lines of code.

See this code example on how to do that where also code examples for the next sections are included.

Speed mode vs. Hybrid mode vs. Flexible mode

GraphHopper offers three different choices of algorithms for routing: The speed mode (which implements Contraction Hierarchies (CH) and is by far the fastest), the hybrid mode (which uses Landmarks (LM) and is still fast, but also supports some features CH does not support) and the flexible mode (Dijkstra or A*) which does not require calculating index data and offers full flexibility and is comparable slow especially for longer routes.

See the profiles for an explanation how to configure the different routing modes. At query time you can disable speed mode using ch.disable=true. In this case either hybrid mode (if there is an LM preparation for the chosen profile) or flexible mode will be used. To use flexible mode in the presence of an LM preparation you need to also set lm.disable=true.

To calculate a route you have to pick one vehicle and optionally an algorithm like bidirectional_astar, see the test speedModeVersusHybridMode.

Heading

The flexible and hybrid modes allow adding a desired heading (north based azimuth between 0 and 360 degree) to any point. Adding a heading makes it more likely that a route starts towards the provided direction, because roads going into other directions are penalized. See heading for an example and more information.

Alternative Routes

For all modes you can calculate alternative routes. Note, that the algorithm for CH is different to the algorithm used for LM or flexible mode. In all caases this setting will affect the speed of your routing requests. See the test headingAndAlternativeRoute and the Parameters class for further hints.

Java client (client-hc)

If you want to calculate routes using the GraphHopper Directions API or a self hosted instance of GraphHopper, you can use the Java and Android client-hc:

GraphHopperAPI gh = new GraphHopperWeb();
gh.load("http://your-graphhopper-service.com");

// or for the GraphHopper Directions API https://graphhopper.com/#directions-api
// gh.load("https://graphhopper.com/api/1/route");

GHResponse rsp = gh.route(new GHRequest(...));

There are also clients for Java Script and other languages).