I’m developing a real-time tracking application using ASP.NET Core with SignalR and the Google Maps JavaScript API. My app displays markers and a computed route on the map that connects two types of users. The marker color changes (for example, red for inactive, yellow for active, purple for cancelled) based on flags that are stored in my own database.
The expected behavior is that when a user refreshes the page, the map should be rebuilt exactly as it was before the refresh using only the persisted database state. In practice, after a refresh, although the marker positions are restored, the computed route disappears and, in one scenario, a marker that was previously showing as yellow reverts to red—even though the database still indicates an active state.
The controller is sending the current flag values (e.g., for an active trip) and stored locations via ViewBag.
I force the persisted flag values to lowercase before comparing them in my JavaScript.
My client-side logic correctly computes the route when the page loads in initial tests, but after a refresh the route no longer appears even though the stored values are intact.
I attempted to persist the displayed route and marker colors on my Google Maps dashboard by relying solely on state saved to my own database. In my ASP.NET Core controllers, I pass persisted values for the student’s active trip, cancelled trip, and the driver’s last location via the ViewBag. Then, in my Razor view’s JavaScript, I force these boolean flags to lowercase and compare them to “true” in order to decide which marker icon to display. For example, in my student dashboard, I initialize the flag like this:
var studentActiveTrip = ('@(ViewBag.StudentActiveTrip != null ? ViewBag.StudentActiveTrip.ToString().ToLower() : "false")' === 'true');
var studentCancelledTrip = ('@(ViewBag.StudentCancelledTrip != null ? ViewBag.StudentCancelledTrip.ToString().ToLower() : "false")' === 'true');
Later, when initializing the map, I check these flags to determine the correct marker color:
if (storedLat && storedLng) {
let lat = parseFloat(storedLat);
let lng = parseFloat(storedLng);
let pos = { lat: lat, lng: lng };
map.setCenter(pos);
map.setZoom(posZoom);
// Determine which icon to display based on the persisted state.
let iconUrl = "http://maps.google.com/mapfiles/ms/icons/red-dot.png";
if (studentActiveTrip) {
iconUrl = "http://maps.google.com/mapfiles/ms/icons/yellow-dot.png";
window.tripActive = true;
} else if (studentCancelledTrip) {
iconUrl = "http://maps.google.com/mapfiles/ms/icons/purple-dot.png";
window.tripActive = false;
}
window.studentMarker = new google.maps.Marker({
position: pos,
map: map,
icon: iconUrl
});
}
Even though my controller is sending the correct state (for example, ActiveTrip is true), when I refresh the page the student’s marker reverts to red and the computed route disappears—despite the fact that the data in the database remains unchanged.
I expected that on a refresh the map would be rebuilt exactly as it was before, so that if ActiveTrip is true in the database the student’s marker would remain yellow and the route between the student and the driver (if the driver’s location is available) would be re-calculated and displayed.