Changeset 567 in tmcsimulator for trunk/webapps/GTEC


Ignore:
Timestamp:
01/24/2020 12:54:29 PM (6 years ago)
Author:
jdalbey
Message:

GTEC vdsLayer.js Add color buttons.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/webapps/GTEC/js/vdsLayer.js

    r566 r567  
    11var postmileList = [];  // the list of postmiles saved from the json file. 
    2 var chosenColor = null; 
    3  
     2var chosenColor = null; // stores color that user picks 
     3 
     4// function to find differences between two arrays 
    45Array.prototype.diff = function(a) { 
    56    return this.filter(function(i) {return a.indexOf(i) < 0;}); 
     
    5253            }; 
    5354        }); 
    54         var clicked = false; 
    55         var first = []; 
    56         var asc = false; 
    57         var color_arr = []; 
    58         var white_arr = []; 
     55        var clicked = false;  
     56        var first = []; // stores first dot 
     57        var asc = false; //ascending 
     58        //var color_arr = [];  
     59        var white_arr = []; //stores all white dots 
    5960        map.data.addListener('click', function(event)  
    6061        { 
    6162            var event_arr = event.feature.getId().split(/[ ,]+/); 
    62             // var start = parseFloat(event[4]); // extract postmile field 
     63            // if user has already made the first click and the second click is  
     64            // on the same highway and same direction 
    6365            if (clicked && event_arr[0] === first[0] && event_arr[1] === first[1]) 
    6466            { 
    65                 // console.log('clicked ' + first + "/" + event_arr + event.feature); 
     67                // compute range between the two clicked dots 
    6668                var range = Math.abs(parseFloat(event_arr[2]) - parseFloat(first[2])); 
    6769                var clicked_dots = []; 
     70                // checks highway direction to determine if postmiles are ascending or descending  
    6871                if (parseFloat(event_arr[2]) < parseFloat(first[2])) 
    6972                { 
     
    7477                    asc = true; 
    7578                } 
     79                // searches map for all dots between the two clicked dots and adds it to list "clicked_dots" 
    7680                map.data.forEach(function (marker)  
    7781                { 
    7882                    var marker_arr = marker.getId().split(/[ ,]+/); 
     83                    // if the dot in the map matches the highway and direction of the clicked dot 
    7984                    if (marker_arr[0] === event_arr[0] && marker_arr[1] === event_arr[1]) 
    8085                    { 
     86                        // compute distance based on direction of highway 
    8187                        if (asc) 
    8288                        { 
     
    8793                            var dist = parseFloat(parseFloat(first[2]) - (marker_arr[2])); 
    8894                        } 
     95                        // if dist within range of the two clicked dots, add to list "clicked_dots" 
    8996                        if (dist <= range && dist >= 0) 
    9097                        { 
     
    93100                    } 
    94101                }); 
    95                 color_arr.push(clicked_dots); 
     102                //color_arr.push(clicked_dots); 
    96103                console.log(clicked_dots); 
    97104                console.log(range); 
     105                // extracts ids for all the white dots 
    98106                var white_arr_ids = white_arr.map(function (marker) { 
    99107                    return marker.id; 
    100108                }); 
     109                // extracts ids for all the clicked dots 
    101110                var clicked_dots_ids = clicked_dots.map(function (marker) { 
    102111                    return marker.getId(); 
    103112                }); 
     113                // computes difference between the white dots and clicked dots to find the unused dots 
    104114                var unused_ids = white_arr_ids.diff(clicked_dots_ids); 
    105115                console.log("Unused: ",unused_ids, "White: ",white_arr_ids); 
     116                // change the dot color of all the unused white dots back to their previous color 
    106117                unused_ids.forEach(function (id){ 
    107118                    var marker = white_arr.filter(function (dot) { 
     
    112123                }); 
    113124                white_arr = []; 
     125                // change the color of all the dots between the two clicked dots to the user picked color 
    114126                if (chosenColor) 
    115127                { 
     
    131143                xhr.send("msg="+lineOut + "\n") 
    132144            } 
     145            // if user has not yet clicked on a dot 
    133146            else if (!clicked) 
    134147            { 
     
    171184                } 
    172185            } 
     186            // if the user's second click is made on a dot not on the first dot's highway 
    173187            else  
    174188            { 
     
    249263    } 
    250264 
    251     // Builds array to store the previous colors of the targetDots 
    252     /*function storePrev(marker, targetMarkers)  
    253     { 
    254         target = marker.id; 
    255         newColor = marker.properties.color; 
    256         // see if new color is different than current color 
    257         currentFeature = map.data.getFeatureById(target); 
    258         currentColor = currentFeature.getProperty("color"); 
    259         // if a new color is desired then assign it to the feature's color property 
    260         if (currentColor != newColor) 
    261         { 
    262             targetMarkers.push({"id" : currentFeature.getId(), 
    263                 "properties" : {"color" : currentColor}}); 
    264         } 
    265     }*/ 
    266  
    267265    // Load the highways dynamic json file and update the map 
    268266    function updateVDSlayer() 
     
    288286    } 
    289287 
     288    // gets the starting letter of a given color 
    290289    function getColorAbbr(str){ 
    291290        if (str === "red") 
     
    297296    } 
    298297 
    299 // Parses the traffic events file and builds an array of target dots for each line 
    300     /*function processVDS() { 
    301         var parsed_JSON; 
    302         loadJSON(kVDSstatusFile, function(response) 
    303         {  
    304             // Parse JSON string into object 
    305             parsed_JSON = JSON.parse(response); 
    306             // Process each new marker - lookup in current map 
    307             var parsed_features =  parsed_JSON.features; 
    308             var eventsFile = ''; 
    309             var xmlhttp = new XMLHttpRequest(); 
    310             xmlhttp.onreadystatechange = function(){ 
    311                 if(xmlhttp.status == 200 && xmlhttp.readyState == 4){ 
    312                     eventsFile = xmlhttp.responseText; 
    313                     var lines = eventsFile.toString().split("\n"); 
    314                     for (var line = 1; line < lines.length; line++) { 
    315                         if (lines[line][0] !== '#') //ignores lines with # 
    316                         { 
    317                             var dots = []; 
    318                             var event = lines[line].split(/[ ,]+/); 
    319                             var start = parseFloat(event[4]); // extract postmile field 
    320                             var range = parseFloat(event[5]); // extract distance 
    321                             var newColor = getColorName(event[6]); // extract DotColor 
    322                             eventTimes.push(event[1]); 
    323                             parsed_features.forEach(function (marker) { 
    324                                 var id_arr = marker.id.split(" "); 
    325                                 if (id_arr[0] === event[2] && id_arr[1] === event[3]){ 
    326                                     var dist = parseFloat(id_arr[2]) - start; //computes difference in postmiles 
    327                                     if (dist <= range && dist >= 0){ // adds all dots within computed range 
    328                                         dots.push({"marker": marker, "color": newColor}); 
    329                                     } 
    330                                 } 
    331                             }); 
    332                             targetDots.push(dots); 
    333                         } 
    334                     } 
    335                 } 
    336             }; 
    337             xmlhttp.open("GET",trafficEventsFile,true); //read traffic events text file 
    338             xmlhttp.send(); 
    339         }); 
    340     } 
    341  
    342     // Increments the eventIndex from and updates all the targetDots on map 
    343     function updateForwards() { 
    344         eventIndex++; 
    345         // limit eventIndex to length of targetDots 
    346         if (eventIndex >= targetDots.length)  
    347         { 
    348             eventIndex = targetDots.length - 1; 
    349         } 
    350         // update all the target dots 
    351         if  (targetDots[eventIndex] !== undefined) { 
    352             targetDots[eventIndex].forEach(function(item){ 
    353             item.marker.properties.color = item.color; 
    354             updateMarker(item.marker); 
    355             }); 
    356         } 
    357     } 
    358  
    359     // Decrements the eventIndex and updates all the targetDots from diff_arr on map 
    360     function updateBackwards() { 
    361         //update each target dot in order to revert to previous color 
    362         if  (diff_arr[eventIndex] !== undefined) { 
    363             diff_arr[eventIndex].forEach(function(item){ 
    364                 updateMarker(item); 
    365             }); 
    366         } 
    367         eventIndex--;  
    368         // restrict eventIndex to -1 
    369         if (eventIndex < -1) 
    370         { 
    371             eventIndex = -1; 
    372         } 
    373     } 
    374      
    375     // Calls updateMarker for all targetDots after storing the previous marker in diff_arr  
    376     function buildDiff() { 
    377         for (var i = 0; i <= targetDots.length; i++) 
    378         { 
    379             if  (targetDots[i] !== undefined) 
    380             { 
    381                 var targetMarkers = new Array(); 
    382                 targetDots[i].forEach(function(item){ 
    383                     item.marker.properties.color = item.color; 
    384                     storePrev(item.marker, targetMarkers); 
    385                     updateMarker(item.marker); 
    386                 }); 
    387                 diff_arr.push(targetMarkers); 
    388             } 
    389         } 
    390     } 
    391 */ 
    392 /*function initVDSbutton() 
    393 { 
    394  
    395     var vdsBtnDiv = document.getElementById('vdsButton'); 
    396     map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(vdsBtnDiv) 
    397     vdsBtnDiv.title = 'Click to toggle vds view'; 
    398  
    399     // Setup the click event listeners to toggle icon display 
    400     vdsBtnDiv.addEventListener('click', function() 
    401     { 
    402         vds_showing = !vds_showing; 
    403         // reveal or hide all the dots 
    404         map.data.forEach(function(feature) 
    405         { 
    406             map.data.overrideStyle(feature, 
    407             { 
    408                 visible: vds_showing 
    409             }); 
    410         }); 
    411         // Determine which button image to show 
    412         if (vds_showing) 
    413         { 
    414             pic = "images/btnDepressed_VDS.png" 
    415         } 
    416         else 
    417         { 
    418             pic = "images/btnReady_VDS.png" 
    419         } 
    420         document.getElementById('vdsBtnImg').src = pic; 
    421     }); 
    422 }*/ 
    423  
    424298function initColorButtons() 
    425299{ 
     300    // add the respective color buttons onto the map 
    426301    var redColor = document.getElementById('redButton'); 
    427302    var greenColor = document.getElementById('greenButton'); 
     
    430305    map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(greenColor); 
    431306    map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(yellowColor); 
     307    // store the user picked color in "chosenColor" 
    432308    redColor.addEventListener('click', function() { 
    433309        chosenColor = "red"; 
     
    440316    }); 
    441317} 
    442  
    443 // init beginning, forward and back buttons as well as time display on map 
    444 /*function initControlButtons() 
    445 { 
    446     var i = 0; 
    447     var time = document.getElementById('time'); 
    448     map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(time) 
    449     var start = document.getElementById('start'); 
    450     map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(start) 
    451     start.title = 'Click to see the first event'; 
    452     var forward = document.getElementById('forward'); 
    453     map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(forward) 
    454         var backward = document.getElementById('backward'); 
    455         map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(backward) 
    456     forward.title = 'Click to see the next event'; 
    457     forward.addEventListener('click', function() { 
    458         updateForwards(); 
    459         backward.disabled = false; 
    460         if (i < eventTimes.length - 1) // get the next eventTime  
    461         { 
    462             ++i; 
    463             time.innerHTML = eventTimes[i]; 
    464         } 
    465         if (eventIndex === targetDots.length - 1) // disable next button if last event reached 
    466         { 
    467             forward.disabled = true; 
    468         } 
    469     }); 
    470         backward.addEventListener('click', function() { 
    471         updateBackwards(); 
    472         forward.disabled = false; 
    473         if (i > 0)  // get the prev eventTime  
    474         { 
    475             --i; 
    476             time.innerHTML = eventTimes[i]; 
    477         } 
    478         if (eventIndex < 0) // disable back button if at first event  
    479         { 
    480             backward.disabled = true; 
    481         } 
    482         }); 
    483     start.addEventListener('click', function() { 
    484         updateVDSlayer(); // redraw markers on map from startup file 
    485         i = 0; 
    486         time.innerHTML = "00:00:00"; 
    487     }); 
    488 } 
    489 */ 
Note: See TracChangeset for help on using the changeset viewer.