@@ -10,10 +10,11 @@ export const TRAFFIC_COLORS = {
1010} ;
1111
1212export 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 }
0 commit comments