@@ -24,6 +24,8 @@ function MapComponent({
2424 setCenterOnLocation,
2525 setRenderMarkerOnMap,
2626 filters,
27+ useResponseLocation,
28+ setUseResponseLocation,
2729} ) {
2830 const { tripLogs, taskLogs, jwt, projectId, mapId } = logData ;
2931
@@ -112,6 +114,7 @@ function MapComponent({
112114 mapRef . current = map ;
113115
114116 const tripObjects = new TripObjects ( { map, setFeaturedObject, setTimeRange } ) ;
117+ mapDivRef . current . tripObjects = tripObjects ;
115118
116119 const addTripPolys = ( ) => {
117120 const trips = tripLogs . getTrips ( ) ;
@@ -163,15 +166,52 @@ function MapComponent({
163166 } ;
164167 map . controls [ window . google . maps . ControlPosition . TOP_LEFT ] . push ( polylineButton ) ;
165168
169+ const bottomControlsWrapper = document . createElement ( "div" ) ;
170+ bottomControlsWrapper . className = "map-controls-bottom-left" ;
171+
166172 const followButton = document . createElement ( "div" ) ;
167173 followButton . className = "follow-vehicle-button" ;
168174 followButton . innerHTML = `<div class="follow-vehicle-background"></div><svg xmlns="http://www.w3.org/2000/svg" viewBox="-10 -10 20 20" width="24" height="24" class="follow-vehicle-chevron"><path d="M -10,10 L 0,-10 L 10,10 L 0,5 z" fill="#4285F4" stroke="#4285F4" stroke-width="1"/></svg>` ;
169175 followButton . onclick = ( ) => {
170176 log ( "Follow vehicle button clicked." ) ;
171177 recenterOnVehicleWrapper ( ) ;
172- map . setZoom ( 17 ) ;
173178 } ;
174- map . controls [ window . google . maps . ControlPosition . LEFT_BOTTOM ] . push ( followButton ) ;
179+
180+ const toggleContainer = document . createElement ( "div" ) ;
181+ toggleContainer . className = "map-toggle-container" ;
182+
183+ const updateToggleStyles = ( reqActive ) => {
184+ reqBtn . className = reqActive ? "map-toggle-button active" : "map-toggle-button" ;
185+ resBtn . className = reqActive ? "map-toggle-button" : "map-toggle-button active" ;
186+ } ;
187+
188+ const reqBtn = document . createElement ( "button" ) ;
189+ reqBtn . textContent = "Request" ;
190+ reqBtn . className = "map-toggle-button" ;
191+ reqBtn . onclick = ( ) => {
192+ setUseResponseLocation ( false ) ;
193+ updateToggleStyles ( true ) ;
194+ } ;
195+
196+ const resBtn = document . createElement ( "button" ) ;
197+ resBtn . textContent = "Response" ;
198+ resBtn . className = "map-toggle-button" ;
199+ resBtn . onclick = ( ) => {
200+ setUseResponseLocation ( true ) ;
201+ updateToggleStyles ( false ) ;
202+ } ;
203+
204+ const separator = document . createElement ( "div" ) ;
205+ separator . className = "map-toggle-separator" ;
206+
207+ updateToggleStyles ( ! useResponseLocation ) ;
208+
209+ toggleContainer . appendChild ( reqBtn ) ;
210+ toggleContainer . appendChild ( resBtn ) ;
211+
212+ bottomControlsWrapper . appendChild ( followButton ) ;
213+ bottomControlsWrapper . appendChild ( toggleContainer ) ;
214+ map . controls [ window . google . maps . ControlPosition . LEFT_BOTTOM ] . push ( bottomControlsWrapper ) ;
175215
176216 const centerListener = map . addListener (
177217 "center_changed" ,
@@ -245,16 +285,17 @@ function MapComponent({
245285 if ( ! map ) return ;
246286 log ( "recenterOnVehicleWrapper called for follow mode." ) ;
247287
248- let position = null ;
249- if ( selectedRow ?. lastlocation ?. rawlocation ) {
250- position = selectedRow . lastlocation . rawlocation ;
251- } else if ( lastValidPositionRef . current ) {
252- position = lastValidPositionRef . current ;
288+ if ( ! isFollowingVehicle ) {
289+ const locationObj = useResponseLocation ? selectedRow ?. lastlocationResponse : selectedRow ?. lastlocation ;
290+ const position = locationObj ?. location || locationObj ?. rawlocation || lastValidPositionRef . current ;
291+ if ( position ) {
292+ map . setCenter ( { lat : position . latitude , lng : position . longitude } ) ;
293+ map . setZoom ( 17 ) ;
294+ }
253295 }
254296
255- if ( position ) map . setCenter ( { lat : position . latitude , lng : position . longitude } ) ;
256297 setIsFollowingVehicle ( ( prev ) => ! prev ) ;
257- } , [ selectedRow ] ) ;
298+ } , [ selectedRow , useResponseLocation , isFollowingVehicle ] ) ;
258299
259300 useEffect ( ( ) => {
260301 const followButton = document . querySelector ( ".follow-vehicle-button" ) ;
@@ -338,16 +379,13 @@ function MapComponent({
338379 return ;
339380 }
340381
341- const location =
342- _ . get ( selectedRow . lastlocation , "location" ) ||
343- _ . get ( selectedRow . lastlocation , "rawlocation" ) ||
344- _ . get ( selectedRow . lastlocationResponse , "location" ) ;
382+ const locationObj = useResponseLocation ? selectedRow . lastlocationResponse : selectedRow . lastlocation ;
383+ const location = _ . get ( locationObj , "location" ) || _ . get ( locationObj , "rawlocation" ) ;
345384
346385 if ( location ?. latitude && location ?. longitude ) {
347386 const pos = { lat : location . latitude , lng : location . longitude } ;
348387 lastValidPositionRef . current = pos ;
349- const heading =
350- _ . get ( selectedRow . lastlocation , "heading" ) || _ . get ( selectedRow . lastlocationResponse , "heading" ) || 0 ;
388+ const heading = _ . get ( locationObj , "heading" ) || 0 ;
351389
352390 if ( vehicleMarkersRef . current . background ) {
353391 vehicleMarkersRef . current . background . setPosition ( pos ) ;
@@ -396,7 +434,7 @@ function MapComponent({
396434 } ) ;
397435 }
398436
399- const rawLocation = _ . get ( selectedRow . lastlocation , "rawlocation" ) ;
437+ const rawLocation = _ . get ( locationObj , "rawlocation" ) ;
400438 if ( rawLocation ?. latitude && rawLocation ?. longitude ) {
401439 const rawPos = { lat : rawLocation . latitude , lng : rawLocation . longitude } ;
402440 if ( vehicleMarkersRef . current . rawLocation ) {
@@ -429,7 +467,37 @@ function MapComponent({
429467 } else {
430468 Object . values ( vehicleMarkersRef . current ) . forEach ( ( marker ) => marker && marker . setMap ( null ) ) ;
431469 }
432- } , [ selectedRow , isFollowingVehicle ] ) ;
470+ } , [ selectedRow , isFollowingVehicle , useResponseLocation ] ) ;
471+
472+ // Update trip objects when toggle changes
473+ useEffect ( ( ) => {
474+ if ( mapDivRef . current && mapDivRef . current . tripObjects ) {
475+ log ( `Updating TripObjects useResponseLocation to ${ useResponseLocation } ` ) ;
476+ const tripObjects = mapDivRef . current . tripObjects ;
477+ tripObjects . setUseResponseLocation ( useResponseLocation ) ;
478+
479+ // Redraw trips
480+ const trips = tripLogs . getTrips ( ) ;
481+ _ . forEach ( trips , ( trip ) => {
482+ tripObjects . addTripVisuals ( trip , minDate , maxDate ) ;
483+ } ) ;
484+ }
485+ } , [ useResponseLocation , tripLogs , minDate , maxDate ] ) ;
486+
487+ // Update toggle button UI
488+ useEffect ( ( ) => {
489+ const container = document . querySelector ( ".map-toggle-container" ) ;
490+ if ( container ) {
491+ const [ reqBtn , , resBtn ] = container . children ;
492+ if ( reqBtn && resBtn ) {
493+ reqBtn . className = `map-toggle-button${ ! useResponseLocation ? " active" : "" } ` ;
494+ resBtn . className = `map-toggle-button${ useResponseLocation ? " active" : "" } ` ;
495+ }
496+ }
497+ if ( isFollowingVehicle && selectedRow ) {
498+ // Re-center if we are following and the toggle changed
499+ }
500+ } , [ useResponseLocation , isFollowingVehicle , selectedRow , recenterOnVehicleWrapper ] ) ;
433501
434502 const toggleHandlers = useMemo ( ( ) => {
435503 const map = mapRef . current ;
0 commit comments