Changeset 530 in tmcsimulator for trunk/webapps/visualizer/js/vdsLayer.js
- Timestamp:
- 11/23/2019 05:18:32 PM (6 years ago)
- File:
-
- 1 edited
-
trunk/webapps/visualizer/js/vdsLayer.js (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/webapps/visualizer/js/vdsLayer.js
r528 r530 1 // Build a solid colored icon to use instead of the classic pin2 function dotSymbol(color)3 {4 var iconPath = iconVDSwhite;5 if (color == 'red')6 {7 iconPath = iconVDSred;8 }9 else if (color == 'yellow')10 {11 iconPath = iconVDSyellow;12 }13 else if (color == 'lime')14 {15 iconPath = iconVDSgreen;16 }17 return {18 url: iconPath,19 anchor: new google.maps.Point(6, 6)20 };21 }22 23 1 // Load the map data from a json file and style all the points 24 function loadVDSlayer()2 function initializeVDSlayer() 25 3 { 26 4 // Load the static map data and call saveCoords when done … … 46 24 }); 47 25 } 26 27 // Build a solid colored icon to use instead of the classic pin 28 function dotSymbol(color) 29 { 30 var iconPath = iconVDSwhite; 31 if (color == 'red') 32 { 33 iconPath = iconVDSred; 34 } 35 else if (color == 'yellow') 36 { 37 iconPath = iconVDSyellow; 38 } 39 else if (color == 'lime') 40 { 41 iconPath = iconVDSgreen; 42 } 43 return { 44 url: iconPath, 45 anchor: new google.maps.Point(6, 6) 46 }; 47 } 48 48 49 // callback when load GeoJson completes 49 50 // save each feature's Point as the original coordinates for later reference … … 56 57 vds_coords[feature.getId()] = pt; // save the Point in a dictionary 57 58 }); 58 // update the dot colors from the dynamic json data59 updateVDSlayer();60 59 // go adjust the marker coordinates so dots don't overlap 61 60 adjustCoords(calcDistanceFactor()); 61 processVDS(); 62 62 } 63 63 … … 114 114 } 115 115 116 // Load the highways static json file and update the map 117 function resetVDSlayer() 118 { 119 eventIndex = -1; 120 loadJSON(kMapStartupFile, function(response) 121 { 122 // Parse JSON string into object 123 //initialVDSjson = JSON.parse(response); 124 // Process each new marker - lookup in current map 125 var initialVDSjsonFeatures = JSON.parse(response).features 126 initialVDSjsonFeatures.forEach(updateMarker); 127 }); 128 } 129 130 function getColorName(str){ 131 if (str === "R\r" || str === "R") 132 return "red"; 133 else if (str === "Y\r" || str === "Y") 134 return "yellow"; 135 else if (str === "G\r" || str === "G") 136 return "lime"; 137 } 138 139 // Parses the traffic events file and builds an array of target dots for each line 140 function processVDS() { 141 //var parsed_JSON; 142 loadJSON(kMapStartupFile, function(response) 143 { 144 // Parse JSON string into object 145 parsed_JSON = JSON.parse(response); 146 // Process each new marker - lookup in current map 147 var parsed_features = parsed_JSON.features; 148 parsed_features.forEach(updateMarker); 149 var eventsFile = ''; 150 // Request the traffic events data file 151 var xmlhttp = new XMLHttpRequest(); 152 xmlhttp.open("GET",trafficEventsFile,true); // Ask the server for the traffic events file 153 // Establish listener for handling the traffic events once file is read 154 xmlhttp.onreadystatechange = function() 155 { 156 if(xmlhttp.status == 200 && xmlhttp.readyState == 4) 157 { 158 eventsFile = xmlhttp.responseText; 159 // Process the traffic events file 160 var lines = eventsFile.toString().split("\n"); 161 // For each line in the events file 162 for (var line = 1; line < lines.length; line++) 163 { 164 var trimmedLine = lines[line].trim(); 165 if (trimmedLine[0] !== '#') //ignores lines with # 166 { 167 var dots = []; 168 var event = trimmedLine.split(/[ ,]+/); 169 var start = parseFloat(event[4]); // extract postmile field 170 var range = parseFloat(event[5]); // extract distance 171 var newColor = getColorName(event[6]); // extract DotColor 172 eventTimes.push(event[1]); // extract the event time 173 // Process each VDS in the highway network 174 parsed_features.forEach(function (marker) { 175 var marker_fields = marker.id.split(" "); 176 // See if this marker's highway and direction match to this event 177 if (marker_fields[0] === event[2] && marker_fields[1] === event[3]) 178 { //computes difference in postmiles 179 var dist = parseFloat(marker_fields[2]) - start; 180 // If this marker is within computed range 181 if (dist <= range && dist >= 0) 182 { 183 // Add the marker to the dots representing this event 184 dots.push({"marker": marker, "color": newColor}); 185 } 186 } 187 }); 188 targetDots.push(dots); // add this events dots to the list of targetdots 189 } // end if 190 } // end for 191 // After the traffic events are processed, go build the differences array 192 buildDiff(); // side effect: leaves map with last event showing 193 resetVDSlayer(); // restore the map to initial state 194 } 195 }; 196 xmlhttp.send(); 197 }); 198 } 199 200 // Calls updateMarker for all targetDots after storing the previous marker in diff_arr 201 // This function only needs to be done once, during startup. 202 function buildDiff() 203 { 204 for (var i = 0; i <= targetDots.length; i++) 205 { 206 if (targetDots[i] !== undefined) 207 { 208 var targetMarkers = new Array(); 209 targetDots[i].forEach(function(item) 210 { 211 item.marker.properties.color = item.color; 212 storePrev(item.marker, targetMarkers); 213 updateMarker(item.marker); 214 }); 215 diff_arr.push(targetMarkers); 216 } 217 } 218 } 219 // Helper function for BuildDiff 220 // Store the previous colors of the targetDots 221 // so it will available if the user goes "back". 116 222 function storePrev(marker, targetMarkers) 117 223 { … … 129 235 } 130 236 131 // Load the highways dynamic json file and update the map 132 function updateVDSlayer() 133 { 134 index = -1; 135 var parsed_JSON; 136 loadJSON(kVDSstatusFile, function(response) 137 { 138 // Parse JSON string into object 139 parsed_JSON = JSON.parse(response); 140 // Process each new marker - lookup in current map 141 parsed_JSON.features.forEach(updateMarker); 142 }); 143 } 144 145 function getColorName(str){ 146 if (str === "R\r" || str === "R") 147 return "red"; 148 else if (str === "Y\r" || str === "Y") 149 return "yellow"; 150 else if (str === "G\r" || str === "G") 151 return "lime"; 152 } 153 154 function processVDS() { 155 var parsed_JSON; 156 loadJSON(kVDSstatusFile, function(response) 157 { 158 // Parse JSON string into object 159 parsed_JSON = JSON.parse(response); 160 // Process each new marker - lookup in current map 161 var array = parsed_JSON.features; 162 var txt = ''; 163 var xmlhttp = new XMLHttpRequest(); 164 xmlhttp.onreadystatechange = function(){ 165 if(xmlhttp.status == 200 && xmlhttp.readyState == 4){ 166 txt = xmlhttp.responseText; 167 var lines = txt.toString().split("\n"); 168 for (var line = 1; line < lines.length; line++) { 169 filters[line-1] = []; 170 var event = lines[line].split(/[ ,]+/); 171 var start = parseFloat(event[4]); // 7.73 172 var range = parseFloat(event[5]); // 0.5 173 var newColor = getColorName(event[6]); // "R" 174 times.push(event[1]); 175 array.forEach(function (marker) { 176 var id_arr = marker.id.split(" "); 177 if (id_arr[0] === event[2] && id_arr[1] === event[3]){ 178 var dist = parseFloat(id_arr[2]) - start; //difference of postmiles 179 if (dist <= range && dist >= 0){ 180 filters[line-1].push({"marker": marker, "color": newColor}); 181 } 182 } 183 }); 184 } 185 } 186 }; 187 xmlhttp.open("GET",trafficEventsFile,true); //read traffic events text file 188 xmlhttp.send(); 189 }); 190 } 191 192 // updates color for each marker based on color in filters array 193 function updateVDS(forwards) { 194 if (forwards) { 195 index++; 196 if (index >= filters.length) 197 { 198 index = filters.length - 1; 199 } 200 if (filters[index] !== undefined) { 201 filters[index].forEach(function(item){ 202 item.marker.properties.color = item.color; 203 updateMarker(item.marker); 204 }); 205 } 206 } 207 else { 208 if (diff_arr[index] !== undefined) { 209 diff_arr[index].forEach(function(item){ 210 updateMarker(item); 211 }); 212 } 213 index--; 214 if (index < -1) 215 { 216 index = -1; 217 } 218 } 219 } 220 221 function buildDiff() 222 { 223 for (var i = 0; i <= filters.length; i++) 224 { 225 if (filters[i] !== undefined) 226 { 227 var targetMarkers = new Array(); 228 filters[i].forEach(function(item){ 229 item.marker.properties.color = item.color; 230 storePrev(item.marker, targetMarkers); 231 updateMarker(item.marker); 232 }); 233 diff_arr.push(targetMarkers); 234 } 235 } 236 } 237 238 function initVDSbutton() 239 { 240 241 var vdsBtnDiv = document.getElementById('vdsButton'); 242 map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(vdsBtnDiv) 243 vdsBtnDiv.title = 'Click to toggle vds view'; 244 245 // Setup the click event listeners to toggle icon display 246 vdsBtnDiv.addEventListener('click', function() 247 { 248 vds_showing = !vds_showing; 249 // reveal or hide all the dots 250 map.data.forEach(function(feature) 251 { 252 map.data.overrideStyle(feature, 253 { 254 visible: vds_showing 237 238 239 // Increments the eventIndex from and updates all the targetDots on map 240 function updateForwards() { 241 eventIndex++; 242 // limit eventIndex to length of targetDots 243 if (eventIndex >= targetDots.length) 244 { 245 eventIndex = targetDots.length - 1; 246 } 247 // update all the target dots 248 if (targetDots[eventIndex] !== undefined) { 249 targetDots[eventIndex].forEach(function(item){ 250 item.marker.properties.color = item.color; 251 updateMarker(item.marker); 255 252 }); 256 }); 257 // Determine which button image to show 258 if (vds_showing) 259 { 260 pic = "images/btnDepressed_VDS.png" 261 } 262 else 263 { 264 pic = "images/btnReady_VDS.png" 265 } 266 document.getElementById('vdsBtnImg').src = pic; 267 }); 268 } 269 270 // init beginning and next buttons as well as time display on map 271 function initForwardbutton() 253 } 254 } 255 256 // Decrements the eventIndex and updates all the targetDots from diff_arr on map 257 function updateBackwards() { 258 //update each target dot in order to revert to previous color 259 if (diff_arr[eventIndex] !== undefined) { 260 diff_arr[eventIndex].forEach(function(item){ 261 updateMarker(item); 262 }); 263 } 264 eventIndex--; 265 // restrict eventIndex to -1 266 if (eventIndex < -1) 267 { 268 eventIndex = -1; 269 } 270 } 271 272 273 // init beginning, forward and back buttons as well as time display on map 274 function initControlButtons() 272 275 { 273 276 var i = 0; 277 // setup the time display 274 278 var time = document.getElementById('time'); 275 279 map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(time) 276 var start = document.getElementById('start'); 277 map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(start) 278 start.title = 'Click to see the first event'; 280 // set up the "beginning" button 281 var beginning = document.getElementById('beginning'); 282 map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(beginning) 283 beginning.title = 'Click to see the first event'; 284 // set up the "next" button 279 285 var forward = document.getElementById('forward'); 280 286 map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(forward) 287 // set up the "back" button 281 288 var backward = document.getElementById('backward'); 282 289 map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(backward) 283 290 forward.title = 'Click to see the next event'; 284 //console.log(diff_arr); 291 //console.log(targetDots, eventTimes); 292 // Establish the listeners for each button 285 293 forward.addEventListener('click', function() { 286 updateVDS(1); 287 if (i < times.length - 1) 294 updateForwards(); 295 backward.disabled = false; 296 if (i < eventTimes.length - 1) // get the next eventTime 288 297 { 289 298 ++i; 290 time.innerHTML = times[i]; 299 time.innerHTML = eventTimes[i]; 300 } 301 if (eventIndex === targetDots.length - 1) // disable next button if last event reached 302 { 303 forward.disabled = true; 291 304 } 292 305 }); 293 306 backward.addEventListener('click', function() { 294 updateVDS(0); 295 if (i > 0) 307 updateBackwards(); 308 forward.disabled = false; 309 if (i > 0) // get the prev eventTime 296 310 { 297 311 --i; 298 time.innerHTML = times[i]; 312 time.innerHTML = eventTimes[i]; 313 } 314 if (eventIndex < 0) // disable back button if at first event 315 { 316 backward.disabled = true; 299 317 } 300 318 }); 301 start.addEventListener('click', function() {302 updateVDSlayer();319 beginning.addEventListener('click', function() { 320 resetVDSlayer(); // redraw markers on map from startup file 303 321 i = 0; 304 322 time.innerHTML = "00:00:00";
Note: See TracChangeset
for help on using the changeset viewer.
