Skip to content

Commit fbea88f

Browse files
authored
feat: traveled route in grey for traffic polyline (#208)
* build: "no-unused-vars": "warn" * feat: use cloud style with no POI marker distractions * feat: traveled route in grey
1 parent cb4c4f7 commit fbea88f

File tree

5 files changed

+61
-9
lines changed

5 files changed

+61
-9
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"plugins": ["react", "jest"],
2222
"rules": {
2323
"no-var": "error",
24+
"no-unused-vars": "warn",
2425
"prefer-arrow-callback": "error",
2526
"react/prop-types": "off",
2627
"react/jsx-key": "off",

src/Map.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,16 @@ function MyMapComponent(props) {
231231
"request.vehicle.currentroutesegmenttraffic.trafficrendering"
232232
);
233233

234+
const rawLocation = _.get(
235+
props.selectedRow.lastlocation,
236+
"rawlocation"
237+
);
238+
234239
const trafficPolyline = new TrafficPolyline({
235240
path: validWaypoints,
236241
zIndex: 2,
237-
trafficRendering: trafficRendering,
242+
trafficRendering: structuredClone(trafficRendering),
243+
currentLatLng: rawLocation,
238244
map: map,
239245
});
240246
setPolylines((prev) => [...prev, ...trafficPolyline.polylines]);

src/TrafficPolyline.js

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ export const TRAFFIC_COLORS = {
1010
};
1111

1212
export class TrafficPolyline {
13-
constructor({ path, zIndex, trafficRendering, map }) {
13+
constructor({ path, zIndex, trafficRendering, currentLatLng, map }) {
1414
this.polylines = [];
1515
this.path = path;
1616
this.zIndex = zIndex;
17+
this.currentLatLng = currentLatLng;
1718
this.map = map;
1819
this.segments = this.calculateSegments(trafficRendering);
1920
this.createPolylines();
@@ -31,12 +32,56 @@ export class TrafficPolyline {
3132
}
3233

3334
try {
34-
// If no traffic rendering or no road stretches, return a single NO_DATA segment
35-
if (!trafficRendering?.roadstretch?.length) {
35+
if (!trafficRendering) {
36+
trafficRendering = { roadstretch: [] };
37+
}
38+
39+
// Add traveled route as NO_DATA if we have currentLatLng
40+
if (
41+
this.currentLatLng &&
42+
this.currentLatLng.longitude &&
43+
this.currentLatLng.latitude
44+
) {
45+
const line = turf.lineString(
46+
this.path.map((point) => [point.lng, point.lat])
47+
);
48+
const currentPoint = turf.point([
49+
this.currentLatLng.longitude,
50+
this.currentLatLng.latitude,
51+
]);
52+
const startPoint = turf.point(line.geometry.coordinates[0]);
53+
54+
try {
55+
const traveledLine = turf.lineSlice(startPoint, currentPoint, line);
56+
const distanceInMeters =
57+
turf.length(traveledLine, { units: "kilometers" }) * 1000;
58+
59+
// Add the traveled segment at the start of roadstretch array
60+
if (distanceInMeters > 0) {
61+
trafficRendering.roadstretch = [
62+
{
63+
style: "STYLE_NO_DATA",
64+
offsetmeters: 0,
65+
lengthmeters: distanceInMeters,
66+
},
67+
...(trafficRendering.roadstretch || []),
68+
];
69+
70+
log("Added traveled route segment:", {
71+
lengthMeters: distanceInMeters,
72+
segments: trafficRendering.roadstretch.length,
73+
});
74+
}
75+
} catch (error) {
76+
log("Error calculating traveled route segment:", error);
77+
}
78+
}
79+
80+
if (!trafficRendering.roadstretch?.length) {
3681
return [
3782
{
3883
path: this.path,
39-
style: "STYLE_NO_DATA",
84+
style: "STYLE_NORMAL",
4085
},
4186
];
4287
}

src/TrafficPolyline.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ describe("TrafficPolyline", () => {
4848
{ lat: 1, lng: 1 },
4949
];
5050

51-
test("creates NO_DATA polyline when no traffic data provided", () => {
51+
test("creates STYLE_NORMAL polyline when no traffic data provided", () => {
5252
const polyline = new TrafficPolyline({
5353
path: samplePath,
5454
map: {},
5555
});
5656

5757
expect(polyline.polylines.length).toBe(1);
5858
expect(polyline.polylines[0].options.strokeColor).toBe(
59-
TRAFFIC_COLORS.STYLE_NO_DATA
59+
TRAFFIC_COLORS.STYLE_NORMAL
6060
);
6161
});
6262

@@ -69,7 +69,7 @@ describe("TrafficPolyline", () => {
6969

7070
expect(polyline.polylines.length).toBe(1);
7171
expect(polyline.polylines[0].options.strokeColor).toBe(
72-
TRAFFIC_COLORS.STYLE_NO_DATA
72+
TRAFFIC_COLORS.STYLE_NORMAL
7373
);
7474
});
7575

src/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// constants.js
22

33
export const DEFAULT_API_KEY = "";
4-
export const DEFAULT_MAP_ID = "";
4+
export const DEFAULT_MAP_ID = "e6ead35a6ace8599";

0 commit comments

Comments
 (0)