source: tmcsimulator/trunk/webapps/js/vdsLayer.js @ 326

Revision 326, 6.1 KB checked in by jdalbey, 7 years ago (diff)

cptms v0.6.3 decomposed into modules

Line 
1    // Build a solid colored icon to use instead of the classic pin
2    // Use a diamond on N and E directions, circle on S and W directions
3    function dotSymbol(color) //,direction)
4    {
5//        var circle = google.maps.SymbolPath.CIRCLE;
6//        var diamond = 'M -1,0 0,-1 1,0 0,1 z';
7//        var myShape = circle;
8        var iconPath = vdsIconGreen;
9        if (color == 'red')
10        {
11           iconPath = vdsIconRed;
12        }
13        else if (color == 'yellow')
14        {
15            iconPath = vdsIconYellow;
16        }
17        return {
18//            path: iconPath,
19//            icon:
20//                    {
21                        url: iconPath, 
22                        anchor: new google.maps.Point(6, 6)
23//                    };
24//            anchor: new google.maps.Point(6, 6),
25//            scale: 5,
26//            strokeColor: "black", // the border color
27//            strokeWeight: 1, // the border thickness
28//            fillColor: color,
29//            fillOpacity: 1.0
30        };
31    }
32
33    // Load the map data from a json file and style all the points
34    function loadVDSlayer()
35    {
36        // Load the static map data and call saveCoords when done
37        map.data.loadGeoJson(kMapStartupFile, null, saveCoords)
38//        var d = new Date();
39//        var start = d.getTime();
40        // Style the map data by applying the desired properties to each feature (marker)
41        // The function will be called every time a feature's properties are updated.
42        map.data.setStyle(function(feature)
43        {
44            // Get the postmile id
45            var name = feature.getId();
46            // Get the desired color value
47            var ptColor = feature.getProperty("color");
48            var street = feature.getProperty("street");
49            // Build the marker
50            var iconSymbol = dotSymbol(ptColor);
51            // return the StyleOptions
52            return {
53                icon: iconSymbol,
54/*                    icon:
55                    {
56                        url: vdsIconGreen,
57                        anchor: new google.maps.Point(6, 6)
58                    }, */
59                title: name + " @" + street, // set rollover text
60                // set zIndex for slowed traffic to a higher value so they overlap
61                zIndex: colorZvalues[ptColor]
62            };
63        });
64    }
65    // callback when load GeoJson completes
66    // save each feature's Point as the original coordinates for later reference
67    function saveCoords(features)
68    {
69        // Iterate over all the features in the map
70        features.forEach(function(feature)
71        {
72            var pt = feature.getGeometry().get();
73            vds_coords[feature.getId()] = pt; // save the Point in a dictionary
74        });
75        // update the dot colors from the dynamic json data
76        updateVDSlayer();
77        // go adjust the marker coordinates so dots don't overlap
78        adjustCoords(calcDistanceFactor());
79    }
80
81    // magic formula controls distance between dots proportionate to zoom factor
82    function calcDistanceFactor()
83    {
84        // 15 is maximum zoom, the point at which no adjustment is needed
85        return (.0005 * (15 - map.getZoom()));
86    }
87
88    // Adjust the coordinates of dots so they appear side-by-side
89    // The perpendicular vector for each dot has been provided,
90    // so we just need to multiply by a scaling factor (adjAmount)
91    // @param adjAmount amount by which to adjust coordinate
92    function adjustCoords(adjAmount)
93    {
94        // Adjust the NB points a slight amount
95        map.data.forEach(function(feature)
96        {
97            // get the name of the current feature
98            var name = feature.getId();
99            // lookup the original coordinates for this feature
100            var coords = vds_coords[name];
101
102            //retrieve the perpendicular vector (precomputed)
103            var perpx = feature.getProperty("perpx")
104            var perpy = feature.getProperty("perpy")
105                // Make adjustment and save it
106            var myLat = coords.lat() + perpy * adjAmount
107            var myLong = coords.lng() + perpx * adjAmount
108            feature.setGeometry(
109            {
110                lat: myLat,
111                lng: myLong
112            });
113        });
114    }
115
116    // update the color (as needed) for a given marker
117    function updateMarker(marker)
118    {
119        target = marker.id;
120        newColor = marker.properties.color;
121        // see if new color is different than current color
122        currentFeature = map.data.getFeatureById(target);
123        currentColor = currentFeature.getProperty("color");
124        // if a new color is desired then assign it to the feature's color property
125        if (currentColor != newColor)
126        {
127            currentFeature.setProperty("color", newColor);
128            // set zIndex for slowed traffic to a higher value so they overlap
129            currentFeature.setProperty("zIndex", colorZvalues[newColor]);
130        }
131    }
132
133
134    // Load the highways dynamic json file and update the map
135    function updateVDSlayer()
136    {
137        var parsed_JSON;
138        loadJSON(kMapPointsFile, function(response)
139        {
140            // Parse JSON string into object
141            parsed_JSON = JSON.parse(response);
142            // Process each new marker - lookup in current map
143            parsed_JSON.features.forEach(updateMarker);
144        });
145    }
146
147function initVDSbutton()
148{
149
150    var vdsBtnDiv = document.getElementById('vdsButton');
151    map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(vdsBtnDiv)
152    vdsBtnDiv.title = 'Click to toggle vds view';
153
154    // Setup the click event listeners to toggle icon display
155    vdsBtnDiv.addEventListener('click', function()
156    {
157        vds_showing = !vds_showing;
158        // reveal or hide all the dots
159        map.data.forEach(function(feature)
160        {
161            map.data.overrideStyle(feature,
162            {
163                visible: vds_showing
164            });
165        });
166        // Determine which button image to show
167        if (vds_showing)
168        {
169            pic = "images/btnDepressed_VDS.png"
170        }
171        else
172        {
173            pic = "images/btnReady_VDS.png"
174        }
175        document.getElementById('vdsBtnImg').src = pic;
176    });
177}
178
Note: See TracBrowser for help on using the repository browser.