Skip to content

Commit b69d81b

Browse files
fix: Eliminate vehicle marker flicker on update (#251)
* fix: Eliminate vehicle marker flicker on row change * fix: excessive logging for larger files f317f92
1 parent e6dfd40 commit b69d81b

File tree

2 files changed

+77
-45
lines changed

2 files changed

+77
-45
lines changed

src/Map.js

Lines changed: 77 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function MapComponent({
3131
const locationProviderRef = useRef(null);
3232
const bubbleMapRef = useRef({});
3333
const trafficLayerRef = useRef(null);
34-
const dataMakersRef = useRef([]);
34+
const vehicleMarkersRef = useRef({});
3535
const trafficPolylinesRef = useRef([]);
3636
const panoramaRef = useRef(null);
3737
const dynamicMarkersRef = useRef([]);
@@ -210,6 +210,8 @@ function MapComponent({
210210
log("Map.js: Cleanup from initialization useEffect.");
211211
dynamicMarkersRef.current.forEach((m) => m.setMap(null));
212212
dynamicMarkersRef.current = [];
213+
Object.values(vehicleMarkersRef.current).forEach((marker) => marker && marker.setMap(null));
214+
vehicleMarkersRef.current = {};
213215
window.google.maps.event.removeListener(clickListener);
214216
window.google.maps.event.removeListener(centerListener);
215217
window.google.maps.event.removeListener(headingListener);
@@ -293,10 +295,14 @@ function MapComponent({
293295
// Effect for updating selected row vehicle marker
294296
useEffect(() => {
295297
const map = mapRef.current;
296-
dataMakersRef.current.forEach((m) => m.setMap(null));
297-
dataMakersRef.current = [];
298+
if (!map) {
299+
return;
300+
}
298301

299-
if (!map || !selectedRow) return;
302+
if (!selectedRow) {
303+
Object.values(vehicleMarkersRef.current).forEach((marker) => marker && marker.setMap(null));
304+
return;
305+
}
300306

301307
const location =
302308
_.get(selectedRow.lastlocation, "location") ||
@@ -309,58 +315,85 @@ function MapComponent({
309315
const heading =
310316
_.get(selectedRow.lastlocation, "heading") || _.get(selectedRow.lastlocationResponse, "heading") || 0;
311317

312-
const backgroundMarker = new window.google.maps.Marker({
313-
position: pos,
314-
map,
315-
icon: {
316-
path: window.google.maps.SymbolPath.CIRCLE,
317-
fillColor: "#FFFFFF",
318-
fillOpacity: 0.7,
319-
scale: 18,
320-
strokeColor: "#FFFFFF",
321-
strokeWeight: 2,
322-
strokeOpacity: 0.3,
323-
},
324-
zIndex: 9,
325-
});
326-
const chevronMarker = new window.google.maps.Marker({
327-
position: pos,
328-
map,
329-
icon: {
330-
path: "M -1,1 L 0,-1 L 1,1 L 0,0.5 z",
331-
fillColor: "#4285F4",
332-
fillOpacity: 1,
333-
scale: 10,
334-
strokeColor: "#4285F4",
335-
strokeWeight: 1,
336-
rotation: heading,
337-
},
338-
zIndex: 10,
339-
});
340-
dataMakersRef.current.push(backgroundMarker, chevronMarker);
341-
342-
const rawLocation = _.get(selectedRow.lastlocation, "rawlocation");
343-
if (rawLocation?.latitude && rawLocation?.longitude) {
344-
const rawPos = { lat: rawLocation.latitude, lng: rawLocation.longitude };
345-
const rawLocationMarker = new window.google.maps.Marker({
346-
position: rawPos,
318+
if (vehicleMarkersRef.current.background) {
319+
vehicleMarkersRef.current.background.setPosition(pos);
320+
if (!vehicleMarkersRef.current.background.getMap()) {
321+
vehicleMarkersRef.current.background.setMap(map);
322+
}
323+
} else {
324+
vehicleMarkersRef.current.background = new window.google.maps.Marker({
325+
position: pos,
347326
map,
348327
icon: {
349328
path: window.google.maps.SymbolPath.CIRCLE,
350-
fillColor: "#FF0000",
329+
fillColor: "#FFFFFF",
330+
fillOpacity: 0.7,
331+
scale: 18,
332+
strokeColor: "#FFFFFF",
333+
strokeWeight: 2,
334+
strokeOpacity: 0.3,
335+
},
336+
zIndex: 9,
337+
});
338+
}
339+
340+
if (vehicleMarkersRef.current.chevron) {
341+
vehicleMarkersRef.current.chevron.setPosition(pos);
342+
const icon = vehicleMarkersRef.current.chevron.getIcon();
343+
icon.rotation = heading;
344+
vehicleMarkersRef.current.chevron.setIcon(icon);
345+
if (!vehicleMarkersRef.current.chevron.getMap()) {
346+
vehicleMarkersRef.current.chevron.setMap(map);
347+
}
348+
} else {
349+
vehicleMarkersRef.current.chevron = new window.google.maps.Marker({
350+
position: pos,
351+
map,
352+
icon: {
353+
path: "M -1,1 L 0,-1 L 1,1 L 0,0.5 z",
354+
fillColor: "#4285F4",
351355
fillOpacity: 1,
352-
scale: 2,
353-
strokeColor: "#FF0000",
356+
scale: 10,
357+
strokeColor: "#4285F4",
354358
strokeWeight: 1,
359+
rotation: heading,
355360
},
356-
zIndex: 8,
361+
zIndex: 10,
357362
});
358-
dataMakersRef.current.push(rawLocationMarker);
363+
}
364+
365+
const rawLocation = _.get(selectedRow.lastlocation, "rawlocation");
366+
if (rawLocation?.latitude && rawLocation?.longitude) {
367+
const rawPos = { lat: rawLocation.latitude, lng: rawLocation.longitude };
368+
if (vehicleMarkersRef.current.rawLocation) {
369+
vehicleMarkersRef.current.rawLocation.setPosition(rawPos);
370+
if (!vehicleMarkersRef.current.rawLocation.getMap()) {
371+
vehicleMarkersRef.current.rawLocation.setMap(map);
372+
}
373+
} else {
374+
vehicleMarkersRef.current.rawLocation = new window.google.maps.Marker({
375+
position: rawPos,
376+
map,
377+
icon: {
378+
path: window.google.maps.SymbolPath.CIRCLE,
379+
fillColor: "#FF0000",
380+
fillOpacity: 1,
381+
scale: 2,
382+
strokeColor: "#FF0000",
383+
strokeWeight: 1,
384+
},
385+
zIndex: 8,
386+
});
387+
}
388+
} else if (vehicleMarkersRef.current.rawLocation) {
389+
vehicleMarkersRef.current.rawLocation.setMap(null);
359390
}
360391

361392
if (isFollowingVehicle) {
362393
map.setCenter(pos);
363394
}
395+
} else {
396+
Object.values(vehicleMarkersRef.current).forEach((marker) => marker && marker.setMap(null));
364397
}
365398
}, [selectedRow, isFollowingVehicle]);
366399

src/TripLogs.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ function processRawLogs(rawLogs, solutionType) {
230230
// Update lastKnownState for next iterations
231231
const locToStore = currentLocation?.location || currentLocation?.rawlocation;
232232
if (locToStore) {
233-
log(`processRawLogs: Storing last known location for log at ${newLog.timestamp}`);
234233
lastKnownState.location = _.cloneDeep(locToStore);
235234
lastKnownState.heading = currentLocation.heading ?? lastKnownState.heading;
236235
}

0 commit comments

Comments
 (0)