Skip to content

Commit 791ca02

Browse files
author
Cameron Mace
authored
Fixed up mapmatching API (#507)
* fixed up mapmatching API * removed unused import * fixed mapmatching rx * fixed map matching test
1 parent c5d2525 commit 791ca02

11 files changed

Lines changed: 542 additions & 96 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ directions-fixtures:
113113
-o mapbox/libjava-services/src/test/fixtures/directions_v5_traffic.json
114114

115115
mapmatching-fixtures:
116-
curl "https://api.mapbox.com/matching/v5/mapbox/driving/$(MAP_MATCHING_COORDINATES)?geometries=polyline&access_token=$(MAPBOX_ACCESS_TOKEN)" \
116+
curl "https://api.mapbox.com/matching/v5/mapbox/driving/$(MAP_MATCHING_COORDINATES)?geometries=polyline&language=sv&steps=true&access_token=$(MAPBOX_ACCESS_TOKEN)" \
117117
-o mapbox/libjava-services/src/test/fixtures/mapmatching_v5_polyline.json
118118

119119
optimized-trips-fixtures:

mapbox/libjava-services-rx/src/main/java/com/mapbox/services/api/rx/mapmatching/v5/MapMatchingServiceRx.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public interface MapMatchingServiceRx {
4545
* along the match geometry. Can be one or all of 'duration', 'distance',
4646
* or 'nodes', each separated by ,. See the response object for more
4747
* details on what it is included with annotations.
48+
* @param language Language of returned turn-by-turn text instructions.
4849
* @return The MapMatchingResponse in an Observable wrapper
4950
* @since 2.0.0
5051
*/
@@ -60,5 +61,6 @@ Observable<MapMatchingResponse> getObservable(
6061
@Query("steps") Boolean steps,
6162
@Query("overview") String overview,
6263
@Query("timestamps") String timestamps,
63-
@Query("annotations") String annotations);
64+
@Query("annotations") String annotations,
65+
@Query("language") String language);
6466
}

mapbox/libjava-services-rx/src/main/java/com/mapbox/services/api/rx/mapmatching/v5/MapboxMapMatchingRx.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public Observable<MapMatchingResponse> getObservable() {
6666
builder.getSteps(),
6767
builder.getOverview(),
6868
builder.getTimestamps(),
69-
builder.getAnnotations()
69+
builder.getAnnotations(),
70+
builder.getLanguage()
7071
);
7172

7273
// Done
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.mapbox.services.api.rx;
2+
3+
import com.google.gson.JsonParser;
4+
5+
import java.io.IOException;
6+
import java.nio.charset.Charset;
7+
import java.nio.file.Files;
8+
import java.nio.file.Paths;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class BaseTest {
13+
14+
protected static final double DELTA = 1E-10;
15+
16+
public void compareJson(String json1, String json2) {
17+
JsonParser parser = new JsonParser();
18+
assertEquals(parser.parse(json1), parser.parse(json2));
19+
}
20+
21+
protected String loadJsonFixture(String filename) throws IOException {
22+
byte[] content = Files.readAllBytes(Paths.get(filename));
23+
return new String(content, Charset.forName("utf-8"));
24+
}
25+
26+
protected String loadJsonFixture(String folder, String filename) throws IOException {
27+
byte[] content = Files.readAllBytes(Paths.get("src/test/fixtures/" + folder + "/" + filename));
28+
return new String(content, Charset.forName("utf-8"));
29+
}
30+
31+
}

mapbox/libjava-services-rx/src/test/java/com/mapbox/services/api/rx/mapmatching/v5/MapboxMapMatchingRxTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.mapbox.services.api.ServicesException;
44
import com.mapbox.services.api.mapmatching.v5.MapMatchingCriteria;
55
import com.mapbox.services.api.mapmatching.v5.models.MapMatchingResponse;
6+
import com.mapbox.services.api.rx.BaseTest;
67
import com.mapbox.services.commons.models.Position;
78

89
import org.junit.After;
@@ -12,9 +13,6 @@
1213
import org.junit.rules.ExpectedException;
1314

1415
import java.io.IOException;
15-
import java.nio.charset.Charset;
16-
import java.nio.file.Files;
17-
import java.nio.file.Paths;
1816
import java.util.List;
1917

2018
import io.reactivex.observers.TestObserver;
@@ -29,11 +27,9 @@
2927
/**
3028
* Test Rx support on the Mapbox Map Matching API
3129
*/
30+
public class MapboxMapMatchingRxTest extends BaseTest {
3231

33-
public class MapboxMapMatchingRxTest {
34-
35-
public static final String POLYLINE_FIXTURE =
36-
"../libjava-services/src/test/fixtures/mapmatching_v5_polyline.json";
32+
private static final String POLYLINE_FIXTURE = "../libjava-services/src/test/fixtures/mapmatching_v5_polyline.json";
3733

3834
private static final String ACCESS_TOKEN = "pk.XXX";
3935

@@ -54,7 +50,7 @@ public void setUp() throws IOException {
5450
@Override
5551
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
5652
try {
57-
String body = new String(Files.readAllBytes(Paths.get(POLYLINE_FIXTURE)), Charset.forName("utf-8"));
53+
String body = loadJsonFixture(POLYLINE_FIXTURE);
5854
return new MockResponse().setBody(body);
5955
} catch (IOException ioException) {
6056
throw new RuntimeException(ioException);

mapbox/libjava-services/src/main/java/com/mapbox/services/api/mapmatching/v5/MapMatchingService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public interface MapMatchingService {
4646
* along the match geometry. Can be one or all of 'duration', 'distance',
4747
* or 'nodes', each separated by ,. See the response object for more
4848
* details on what it is included with annotations.
49+
* @param language Language of returned turn-by-turn text instructions.
4950
* @return The MapMatchingResponse in a Call wrapper
5051
* @since 2.0.0
5152
*/
@@ -62,5 +63,6 @@ Call<MapMatchingResponse> getCall(
6263
@Query("steps") Boolean steps,
6364
@Query("overview") String overview,
6465
@Query("timestamps") String timestamps,
65-
@Query("annotations") String annotations);
66+
@Query("annotations") String annotations,
67+
@Query("language") String language);
6668
}

mapbox/libjava-services/src/main/java/com/mapbox/services/api/mapmatching/v5/MapboxMapMatching.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.mapbox.services.api.mapmatching.v5;
22

3-
import com.google.gson.Gson;
43
import com.mapbox.services.api.MapboxBuilder;
54
import com.mapbox.services.api.MapboxService;
65
import com.mapbox.services.api.ServicesException;
@@ -35,7 +34,6 @@ public class MapboxMapMatching extends MapboxService<MapMatchingResponse> {
3534
protected Builder builder = null;
3635
private MapMatchingService service = null;
3736
private Call<MapMatchingResponse> call = null;
38-
private Gson gson;
3937

4038
protected MapboxMapMatching(Builder builder) {
4139
this.builder = builder;
@@ -85,7 +83,8 @@ public Call<MapMatchingResponse> getCall() {
8583
builder.getSteps(),
8684
builder.getOverview(),
8785
builder.getTimestamps(),
88-
builder.getAnnotations()
86+
builder.getAnnotations(),
87+
builder.getLanguage()
8988
);
9089

9190
return call;
@@ -152,6 +151,7 @@ public static class Builder<T extends Builder> extends MapboxBuilder {
152151
private String overview = null;
153152
private String[] timestamps = null;
154153
private String annotations = null;
154+
private String language;
155155

156156
/**
157157
* Constructor
@@ -378,6 +378,27 @@ public T setBaseUrl(String baseUrl) {
378378
return (T) this;
379379
}
380380

381+
/**
382+
* Optionally set the language of returned turn-by-turn text instructions. The default is {@code en} for English.
383+
*
384+
* @param language The locale in which results should be returned.
385+
* @return Builder
386+
* @see <a href="https://www.mapbox.com/api-documentation/#instructions-languages">Supported languages</a>
387+
* @since 2.2.0
388+
*/
389+
public T setLanguage(String language) {
390+
this.language = language;
391+
return (T) this;
392+
}
393+
394+
/**
395+
* @return The language the turn-by-turn directions will be in.
396+
* @since 2.2.0
397+
*/
398+
public String getLanguage() {
399+
return language;
400+
}
401+
381402
/**
382403
* Builder method
383404
*

mapbox/libjava-services/src/main/java/com/mapbox/services/api/mapmatching/v5/models/MapMatchingTracepoint.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@ public class MapMatchingTracepoint extends DirectionsWaypoint {
1010

1111
@SerializedName("matchings_index")
1212
private int matchingsIndex;
13-
13+
@SerializedName("alternatives_count")
14+
private int alternativesCount;
1415
@SerializedName("waypoint_index")
1516
private int waypointIndex;
1617

1718
public MapMatchingTracepoint() {
1819
}
1920

21+
public MapMatchingTracepoint(int matchingsIndex, int alternativesCount, int waypointIndex) {
22+
this.matchingsIndex = matchingsIndex;
23+
this.alternativesCount = alternativesCount;
24+
this.waypointIndex = waypointIndex;
25+
}
26+
2027
/**
2128
* Index to the match object in matchings the sub-trace was matched to.
2229
*
@@ -52,4 +59,26 @@ public int getWaypointIndex() {
5259
public void setWaypointIndex(int waypointIndex) {
5360
this.waypointIndex = waypointIndex;
5461
}
62+
63+
/**
64+
* Number of probable alternative matchings for this trace point. A value of zero indicates that this point was
65+
* matched unambiguously. Split the trace at these points for incremental map matching.
66+
*
67+
* @return an integer representing the alternatives count
68+
* @since 2.2.0
69+
*/
70+
public int getAlternativesCount() {
71+
return alternativesCount;
72+
}
73+
74+
/**
75+
* Set the number of probable alternative matchings for this trace point. A value of zero indicates that this point
76+
* was matched unambiguously. Split the trace at these points for incremental map matching.
77+
*
78+
* @param alternativesCount an integer representing the alternatives count
79+
* @since 2.2.0
80+
*/
81+
public void setAlternativesCount(int alternativesCount) {
82+
this.alternativesCount = alternativesCount;
83+
}
5584
}

0 commit comments

Comments
 (0)