1+ /* eslint-disable no-undef, no-use-before-define, react-hooks/exhaustive-deps, array-callback-return */
2+
13import React , { useEffect , useState , useCallback } from 'react' ;
24import { View , Text , Alert , TouchableOpacity , Switch } from 'react-native' ;
35import MapView , { Polyline , Marker } from 'react-native-maps' ;
@@ -11,7 +13,8 @@ export default function DisplayRouteScreen({ navigation, route }) {
1113 const { origin, destination, routeData, polylineCoordinates } = route . params ;
1214 const [ location , setLocation ] = useState ( null ) ;
1315 const [ travelledPolyline , setTravelledPolyline ] = useState ( [ ] ) ;
14- const [ remainingPolyline , setRemainingPolyline ] = useState ( polylineCoordinates ) ;
16+ const [ remainingPolyline , setRemainingPolyline ] =
17+ useState ( polylineCoordinates ) ;
1518 const [ devMode , setDevMode ] = useState ( false ) ;
1619 const [ detailedStepData , setDetailedStepData ] = useState ( [ ] ) ;
1720 const [ currentInstruction , setCurrentInstruction ] = useState ( null ) ;
@@ -20,7 +23,7 @@ export default function DisplayRouteScreen({ navigation, route }) {
2023 getStepData ( ) ;
2124 setDetailedStepData ( [ ] ) ;
2225 getDetailedStepData ( ) ;
23- } , [ ] ) ;
26+ } , [ getDetailedStepData , routeData ] ) ;
2427
2528 // Check proximity to the next coordinate in the polyline
2629
@@ -29,12 +32,14 @@ export default function DisplayRouteScreen({ navigation, route }) {
2932 // Start tracking location
3033 const startTracking = async ( ) => {
3134 try {
32- locationSubscription = await startLocationTracking ( ( currentLocation ) => {
33- if ( ! devMode ) {
34- setLocation ( currentLocation ) ;
35- checkProximityAndUpdate ( currentLocation ) ;
36- }
37- } ) ;
35+ locationSubscription = await startLocationTracking (
36+ ( currentLocation ) => {
37+ if ( ! devMode ) {
38+ setLocation ( currentLocation ) ;
39+ checkProximityAndUpdate ( currentLocation ) ;
40+ }
41+ } ,
42+ ) ;
3843 } catch ( error ) {
3944 console . error ( 'Error starting location tracking:' , error ) ;
4045 }
@@ -50,7 +55,7 @@ export default function DisplayRouteScreen({ navigation, route }) {
5055 } , [ devMode , checkProximityAndUpdate ] ) ;
5156
5257 function removeHtmlTags ( instruction ) {
53- return instruction . replace ( / < \/ ? [ ^ > ] + ( > | $ ) / g, "" ) ;
58+ return instruction . replace ( / < \/ ? [ ^ > ] + ( > | $ ) / g, '' ) ;
5459 }
5560
5661 // Creating a dictionary of step data for each step in the route
@@ -59,45 +64,51 @@ export default function DisplayRouteScreen({ navigation, route }) {
5964 const getDetailedStepData = ( ) => {
6065 routeData . legs [ 0 ] . steps . map ( ( step ) => {
6166 // Due to the complicated nature of the response some modes of transport have instructions in outer steps and other in inner
62- if ( step . travel_mode !== 'TRANSIT' && step . steps ) { // For non-transit steps within a transit route
63- step . steps . map ( ( detailedStep ) => {
64- let temp = detailedStepData ;
67+ if ( step . travel_mode !== 'TRANSIT' && step . steps ) {
68+ // For non-transit steps within a transit route
69+ step . steps . foreach ( ( detailedStep ) => {
70+ const temp = detailedStepData ;
6571 temp . push ( {
6672 html_instructions : removeHtmlTags ( detailedStep . html_instructions ) ,
6773 start_location : detailedStep . start_location ,
6874 } ) ;
6975 setDetailedStepData ( temp ) ;
70- } )
71- } else if ( step . travel_mode === 'TRANSIT' ) { // If the step is a transit step, add the arrival stop to the instructions
72- let temp = detailedStepData ;
76+ } ) ;
77+ } else if ( step . travel_mode === 'TRANSIT' ) {
78+ // If the step is a transit step, add the arrival stop to the instructions
79+ const temp = detailedStepData ;
7380 temp . push ( {
7481 html_instructions : `${ removeHtmlTags ( step . html_instructions ) } until ${ step . transit_details . arrival_stop . name } ` ,
7582 start_location : step . start_location ,
7683 } ) ;
7784 setDetailedStepData ( temp ) ;
78- } else { // For non transit routes
79- let temp = detailedStepData ;
85+ } else {
86+ // For non transit routes
87+ const temp = detailedStepData ;
8088 temp . push ( {
8189 html_instructions : removeHtmlTags ( step . html_instructions ) ,
8290 start_location : step . start_location ,
8391 } ) ;
8492 setDetailedStepData ( temp ) ;
8593 }
94+ return null ;
8695 } ) ;
8796 } ;
8897
89-
9098 const checkProximityAndUpdate = useCallback (
9199 ( currentLocation ) => {
92- for ( let i = 0 ; i < remainingPolyline . length ; i ++ ) {
100+ for ( let i = 0 ; i < remainingPolyline . length ; i += 1 ) {
93101 const distance = haversine ( currentLocation , remainingPolyline [ i ] ) ;
94102
95103 // Assuming 50 meters as the proximity threshold
96104 if ( distance < 50 ) {
97- setTravelledPolyline ( ( prev ) => [ ...prev , ...remainingPolyline . slice ( 0 , i ) ] ) ;
105+ setTravelledPolyline ( ( prev ) => [
106+ ...prev ,
107+ ...remainingPolyline . slice ( 0 , i ) ,
108+ ] ) ;
98109 setRemainingPolyline ( ( prev ) => prev . slice ( i ) ) ;
99110
100- if ( remainingPolyline . length == 1 ) {
111+ if ( remainingPolyline . length === 1 ) {
101112 Alert . alert (
102113 'Destination reached' ,
103114 'You have reached your destination.' ,
@@ -106,21 +117,26 @@ export default function DisplayRouteScreen({ navigation, route }) {
106117 navigation . navigate ( 'Map' ) ;
107118 } else {
108119 // Update instruction if previous instruction complete
109- for ( let i = 0 ; i < detailedStepData . length ; i ++ ) {
110- let instructionLocation = { latitude : detailedStepData [ i ] . start_location . lat ,
111- longitude : detailedStepData [ i ] . start_location . lng } ;
112- const instructionDistance = haversine ( currentLocation , instructionLocation ) ;
120+ for ( let j = 0 ; j < detailedStepData . length ; j += 1 ) {
121+ const instructionLocation = {
122+ latitude : detailedStepData [ j ] . start_location . lat ,
123+ longitude : detailedStepData [ j ] . start_location . lng ,
124+ } ;
125+ const instructionDistance = haversine (
126+ currentLocation ,
127+ instructionLocation ,
128+ ) ;
113129 if ( instructionDistance < 50 ) {
114- setCurrentInstruction ( detailedStepData [ i ] . html_instructions ) ;
115- setDetailedStepData ( ( prev ) => prev . slice ( i ) ) ;
130+ setCurrentInstruction ( detailedStepData [ j ] . html_instructions ) ;
131+ setDetailedStepData ( ( prev ) => prev . slice ( j ) ) ;
116132 }
117133 }
118134 }
119135 break ;
120136 }
121137 }
122138 } ,
123- [ remainingPolyline , navigation ] ,
139+ [ remainingPolyline , navigation , detailedStepData ] ,
124140 ) ;
125141
126142 // Call checkProximityAndUpdate and update setlocation on latitude / longitude button click
@@ -151,8 +167,9 @@ export default function DisplayRouteScreen({ navigation, route }) {
151167 { currentInstruction != null ? (
152168 < Text style = { displayRouteStyles . routeInfo } >
153169 { currentInstruction }
154- </ Text > ) :
155- ( < >
170+ </ Text >
171+ ) : (
172+ < >
156173 < Text style = { displayRouteStyles . routeInfo } >
157174 Route from { origin } to { destination }
158175 </ Text >
@@ -162,7 +179,8 @@ export default function DisplayRouteScreen({ navigation, route }) {
162179 < Text style = { displayRouteStyles . routeInfo } >
163180 Duration: { routeData . legs [ 0 ] . duration . text }
164181 </ Text >
165- </ > ) }
182+ </ >
183+ ) }
166184 </ View >
167185 < View style = { displayRouteStyles . devModeContainer } >
168186 < Text style = { displayRouteStyles . devModeText } > Dev Mode</ Text >
0 commit comments