Skip to content

Commit 835b944

Browse files
authored
move Java code from API documentation to code (graphhopper#2180)
* move to code * the sync endpoint makes more sense now as default
1 parent 0c3fd78 commit 835b944

File tree

5 files changed

+118
-116
lines changed

5 files changed

+118
-116
lines changed

client-hc/README.md

+13-113
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Handcrafted Java and Android client for the GraphHopper Directions API
22

3-
We have a hand-crafted API client (`client-hc`) for the Matrix and Routing API that is more memory efficient e.g. for large matrices
4-
than the autogenerated client below. The Maven snippet for the handcrafted client is:
3+
We have a hand-crafted API client (`client-hc`) for several API parts like the Matrix and Routing API that is more memory efficient
4+
e.g. for large matrices than the autogenerated client. To use this project
5+
include the following Maven snippet:
56

67
```xml
78
<dependency>
@@ -13,131 +14,30 @@ than the autogenerated client below. The Maven snippet for the handcrafted clien
1314

1415
## Usage
1516

16-
```java
17-
// Hint: create this thread safe instance only once in your application to allow the underlying library to cache the costly initial https handshake
18-
GraphHopperWeb gh = new GraphHopperWeb();
19-
// insert your key here
20-
gh.setKey("YOUR_KEY");
21-
// change timeout, default is 5 seconds
22-
gh.setDownloader(new OkHttpClient.Builder().
23-
connectTimeout(5, TimeUnit.SECONDS).
24-
readTimeout(5, TimeUnit.SECONDS).build());
25-
26-
// specify at least two coordinates
27-
GHRequest req = new GHRequest().
28-
addPoint(new GHPoint(49.6724, 11.3494)).
29-
addPoint(new GHPoint(49.6550, 11.4180));
30-
// Set vehicle like car, bike, foot, ...
31-
req.setVehicle("bike");
32-
// Optionally enable/disable elevation in output PointList, currently bike and foot support elevation, default is false
33-
req.getHints().put("elevation", false);
34-
// Optionally enable/disable turn instruction information, defaults is true
35-
req.getHints().put("instructions", true);
36-
// Optionally enable/disable path geometry information, default is true
37-
req.getHints().put("calc_points", true);
38-
// note: turn off instructions and calcPoints if you just need the distance or time
39-
// information to make calculation and transmission faster
40-
//
41-
// Optionally set specific locale for instruction information, supports already over 25 languages,
42-
// defaults to English
43-
req.setLocale(Locale.GERMAN);
44-
45-
GHResponse fullRes = gh.route(req);
46-
47-
if(res.hasErrors()) {
48-
// handle or throw exceptions res.getErrors()
49-
return;
50-
}
51-
52-
ResponsePath res = fullRes.getBest();
53-
// get path geometry information (latitude, longitude and optionally elevation)
54-
PointList pl = res.getPoints();
55-
// distance of the full path, in meter
56-
double distance = res.getDistance();
57-
// time of the full path, in milliseconds
58-
long millis = res.getTime();
59-
// get information per turn instruction
60-
InstructionList il = res.getInstructions();
61-
```
62-
63-
### Matrix API
64-
65-
```java
66-
// Hint: create this thread safe instance only once in your application to allow the underlying library to cache the costly initial https handshake
67-
GraphHopperMatrixWeb matrixClient = new GraphHopperMatrixWeb();
68-
matrixClient.setKey("[YOUR_KEY]");
69-
70-
GHMRequest ghmRequest = new GHMRequest();
71-
ghmRequest.addOutArray("distances");
72-
ghmRequest.addOutArray("times");
73-
ghmRequest.setVehicle("car");
74-
75-
// init points for a symmetric matrix
76-
List<GHPoint> allPoints = ...;
77-
ghmRequest.addAllPoints(allPoints);
78-
79-
// or init e.g. a one-to-many matrix:
80-
ghmRequest.addFromPoint(from);
81-
for(GHPoint to : toList) {
82-
ghmRequest.addToPoint(to);
83-
}
84-
85-
MatrixResponse response = matrixClient.route(ghmRequest);
86-
GHMResponse singleRsp = response.get(fromIndex, toIndex);
87-
singleRsp.getDistance();
88-
...
89-
```
90-
91-
For small matrices like 20x20 or less locations you can use synchronous calls to reduce latency:
92-
93-
```java
94-
GraphHopperMatrixWeb ghMatrix = new GraphHopperMatrixWeb(new GHMatrixSyncRequester());
95-
```
96-
97-
## Build Latest Development Version
98-
99-
```bash
100-
git clone https://github.com/graphhopper/directions-api-java-client/
101-
cd client
102-
mvn -DskipTests=true clean install
103-
104-
# now execute the test and set your key
105-
mvn -Dgraphhopper.key=[YOUR_KEY] clean test verify
106-
```
107-
108-
## Android
109-
110-
It is important to use this client not on the main thread of Android as it could block the app.
111-
See [issue 7](https://github.com/graphhopper/directions-api-java-client/issues/7) for more information.
112-
113-
# Autogenerated Client
114-
115-
The autogenerated client is available [here](https://github.com/graphhopper/directions-api-clients) and you can refer to this client in your pom.xml via
116-
```xml
117-
<dependency>
118-
<groupId>com.graphhopper</groupId>
119-
<artifactId>directions-api-client</artifactId>
120-
<version>[CURRENT-VERSION]</version>
121-
</dependency>
122-
```
123-
124-
Please [search maven](https://search.maven.org/#search%7Cga%7C1%7Cdirections-api-client-parent) for the latest version.
17+
A real world example for the Routing and Matrix API can be found
18+
[here](./src/test/java/com/graphhopper/api/Examples.java).
12519

12620
## License
12721

12822
Apache License 2.0
12923

13024
## Usage
13125

132-
You can use the API client for many API parts like:
26+
You can use the API client for many API parts:
13327

13428
* the Geocoding API
13529
* the Route Optimization API
13630
* the Routing API
13731
* the Matrix API
13832
* the Isochrone API
13933

140-
Please see the [examples](https://github.com/graphhopper/directions-api-java-client/tree/master/client-examples/src/main/java/com/graphhopper/directions/api/examples) for specific and up-to-date Java code. This client was auto created via a swagger configuration. You can easily do so on your own too with [this repository](https://github.com/graphhopper/directions-api-clients).
34+
For the Route Optimization API you can create the sources from our OpenAPI spec.
35+
See [this repository](https://github.com/graphhopper/directions-api-clients#deprecated-notice) for more details about it.
36+
37+
## Android
38+
39+
It is important to use this client not on the main thread of Android as it could block the app.
40+
See [issue 7](https://github.com/graphhopper/directions-api-java-client/issues/7) for more information.
14141

14242
### Android Alternative
14343

client-hc/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<dependency>
4242
<groupId>com.squareup.okhttp3</groupId>
4343
<artifactId>okhttp</artifactId>
44-
<version>3.14.2</version>
44+
<version>3.14.9</version>
4545
</dependency>
4646
<dependency>
4747
<groupId>org.slf4j</groupId>

client-hc/src/main/java/com/graphhopper/api/GraphHopperMatrixWeb.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ public class GraphHopperMatrixWeb {
1515
private String key;
1616

1717
public GraphHopperMatrixWeb() {
18-
this(new GHMatrixBatchRequester());
18+
this(new GHMatrixSyncRequester());
1919
}
2020

2121
public GraphHopperMatrixWeb(String serviceUrl) {
22-
this(new GHMatrixBatchRequester(serviceUrl));
22+
this(new GHMatrixSyncRequester(serviceUrl));
2323
}
2424

2525
public GraphHopperMatrixWeb(GHMatrixAbstractRequester requester) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.graphhopper.api;
2+
3+
import com.graphhopper.GHRequest;
4+
import com.graphhopper.GHResponse;
5+
import com.graphhopper.ResponsePath;
6+
import com.graphhopper.util.Instruction;
7+
import com.graphhopper.util.InstructionList;
8+
import com.graphhopper.util.PointList;
9+
import com.graphhopper.util.shapes.GHPoint;
10+
import okhttp3.OkHttpClient;
11+
import org.junit.Ignore;
12+
import org.junit.jupiter.api.Test;
13+
14+
import java.util.Arrays;
15+
import java.util.Locale;
16+
import java.util.concurrent.TimeUnit;
17+
18+
@Ignore
19+
public class Examples {
20+
21+
String apiKey = "<YOUR_API_KEY>";
22+
23+
@Test
24+
public void routing() {
25+
// Hint: create this thread safe instance only once in your application to allow the underlying library to cache the costly initial https handshake
26+
GraphHopperWeb gh = new GraphHopperWeb();
27+
// insert your key here
28+
gh.setKey(apiKey);
29+
// change timeout, default is 5 seconds
30+
gh.setDownloader(new OkHttpClient.Builder().connectTimeout(5, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS).build());
31+
32+
// specify at least two coordinates
33+
GHRequest req = new GHRequest().
34+
addPoint(new GHPoint(49.6724, 11.3494)).
35+
addPoint(new GHPoint(49.6550, 11.4180));
36+
// Set vehicle like car, bike, foot, ...
37+
req.putHint("vehicle", "bike");
38+
// Optionally enable/disable elevation in output PointList, currently bike and foot support elevation, default is false
39+
req.putHint("elevation", false);
40+
// Optionally enable/disable turn instruction information, defaults is true
41+
req.putHint("instructions", true);
42+
// Optionally enable/disable path geometry information, default is true
43+
req.putHint("calc_points", true);
44+
// note: turn off instructions and calcPoints if you just need the distance or time
45+
// information to make calculation and transmission faster
46+
47+
// Optionally set specific locale for instruction information, supports already over 25 languages,
48+
// defaults to English
49+
req.setLocale(Locale.GERMAN);
50+
51+
GHResponse fullRes = gh.route(req);
52+
53+
if (fullRes.hasErrors())
54+
throw new RuntimeException(fullRes.getErrors().toString());
55+
56+
// get best path (you will get more than one path here if you requested algorithm=alternative_route)
57+
ResponsePath res = fullRes.getBest();
58+
// get path geometry information (latitude, longitude and optionally elevation)
59+
PointList pl = res.getPoints();
60+
// distance of the full path, in meter
61+
double distance = res.getDistance();
62+
// time of the full path, in milliseconds
63+
long millis = res.getTime();
64+
// get information per turn instruction
65+
InstructionList il = res.getInstructions();
66+
for (Instruction i : il) {
67+
// System.out.println(i.getName());
68+
}
69+
}
70+
71+
@Test
72+
public void matrix() {
73+
// Hint: create this thread safe instance only once in your application to allow the underlying library to cache the costly initial https handshake
74+
GraphHopperMatrixWeb matrixClient = new GraphHopperMatrixWeb();
75+
// for very large matrices you need:
76+
// GraphHopperMatrixWeb matrixClient = new GraphHopperMatrixWeb(new GHMatrixBatchRequester());
77+
matrixClient.setKey(apiKey);
78+
79+
GHMRequest ghmRequest = new GHMRequest();
80+
ghmRequest.addOutArray("distances");
81+
ghmRequest.addOutArray("times");
82+
ghmRequest.putHint("vehicle", "car");
83+
84+
// init points for a symmetric matrix
85+
// List<GHPoint> allPoints = Arrays.asList(new GHPoint(49.6724, 11.3494), new GHPoint(49.6550, 11.4180));
86+
// ghmRequest.addAllPoints(allPoints);
87+
88+
// or init e.g. a one-to-many matrix:
89+
ghmRequest.addFromPoint(new GHPoint(49.6724, 11.3494));
90+
for (GHPoint to : Arrays.asList(new GHPoint(49.6724, 11.3494), new GHPoint(49.6550, 11.4180))) {
91+
ghmRequest.addToPoint(to);
92+
}
93+
94+
MatrixResponse response = matrixClient.route(ghmRequest);
95+
if (response.hasErrors())
96+
throw new RuntimeException(response.getErrors().toString());
97+
98+
// get time from first to second point:
99+
// System.out.println(response.getTime(0, 1));
100+
}
101+
}

core/files/changelog.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
3.0
2+
default for GraphHopperMatrixWeb (client for Matrix API) is now the sync POST request without the artificial polling delay in most cases
23
refactored TransportationMode to better reflect the usage in source data parsing only
34
CustomWeighting requires LinkedHashMap throw exception if this is not provided
45
removed Dockerfile

0 commit comments

Comments
 (0)