/examples/GradientControl/src/GradientControlExample.mxml

http://gmaps-utility-library-flash.googlecode.com/ · Macromedia eXtensible Markup Language · 288 lines · 258 code · 30 blank · 0 comment · 0 complexity · cdea3294efbabf9dec7a635fa6000cc0 MD5 · raw file

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- Copyright 2009 Google Inc.
  3. Deme for a GradientControl Flash Google Maps API element.
  4. See index.html for more information. -->
  5. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
  6. xmlns:gvlt="com.google.maps.extras.gradients.*">
  7. <mx:Script>
  8. <![CDATA[
  9. import flash.net.URLRequest;
  10. import flash.net.URLLoader;
  11. import flash.events.Event;
  12. import com.adobe.serialization.json.JSON;
  13. import com.google.maps.Color;
  14. import com.google.maps.LatLng;
  15. import com.google.maps.MapMouseEvent;
  16. import com.google.maps.MapType;
  17. import com.google.maps.controls.PositionControl;
  18. import com.google.maps.controls.ZoomControl;
  19. import com.google.maps.overlays.EncodedPolylineData;
  20. import com.google.maps.overlays.Polygon;
  21. import com.google.maps.overlays.PolygonOptions;
  22. import com.google.maps.overlays.Polyline;
  23. import com.google.maps.overlays.PolylineOptions;
  24. import com.google.maps.styles.FillStyle;
  25. import com.google.maps.styles.StrokeStyle;
  26. import com.google.maps.extras.gradients.GradientControl;
  27. import com.google.maps.extras.gradients.GradientRule;
  28. import com.google.maps.extras.gradients.GradientRuleList;
  29. import com.google.maps.extras.gradients.MultiPolygonWithValue;
  30. import com.google.maps.extras.gradients.ThematicOverlay;
  31. private var valueDict:Object = {};
  32. private var geometries:Object = [];
  33. private var borders:Array = [];
  34. private var ruleList:GradientRuleList = null;
  35. private var thematicOverlay:ThematicOverlay = null;
  36. private var selectedCountry:String = "Russia";
  37. private function dataLoaded(event:Event):void {
  38. var loader:URLLoader = URLLoader(event.target);
  39. valueDict = JSON.decode(loader.data);
  40. for each(var p:MultiPolygonWithValue in geometries) {
  41. if (p.id in valueDict) {
  42. p.amount = valueDict[p.id];
  43. }
  44. }
  45. countryClicked("Russia", null);
  46. borders = [0, 10, 20, 50];
  47. borderSlider1.value = borders[1];
  48. borderSlider2.value = borders[2];
  49. createGradients();
  50. thematicOverlay = new ThematicOverlay(map, ruleList, geometries);
  51. thematicOverlay.draw();
  52. borderSlider1.enabled = true;
  53. borderSlider2.enabled = true;
  54. countrySlider.enabled = true;
  55. }
  56. private function createGradients():void {
  57. var g1:GradientRule = new GradientRule();
  58. g1.maxValue = borders[1];
  59. g1.minColor = Color.GREEN;
  60. g1.maxColor = Color.YELLOW;
  61. var g2:GradientRule = new GradientRule();
  62. g2.maxColor = 0xff7700;
  63. g2.maxValue = borders[2];
  64. var g3:GradientRule = new GradientRule();
  65. g3.maxValue = borders[3];
  66. g3.maxColor = 0xff0000;
  67. ruleList = new GradientRuleList([g1, g2, g3]);
  68. gradientBar.borders = borders;
  69. gradientBar.drawGradientRule(ruleList);
  70. }
  71. // From http://stackoverflow.com/questions/359635/flex-implementing-classic-curry-function-in-actionscript
  72. public static function curry(func:Function, ... args:Array):*
  73. {
  74. var arity:int = func.length;
  75. var currying:Function = function(func:Function, arity:int, args:Array):*
  76. {
  77. return function(... moreArgs:Array):* {
  78. if(moreArgs.length + args.length < arity)
  79. {
  80. return currying(func, arity, args.concat(moreArgs));
  81. }
  82. return func.apply(this, args.concat(moreArgs));
  83. }
  84. }
  85. return currying(func, arity, args);
  86. }
  87. private function countryClicked(country:String, event:MapMouseEvent):void {
  88. selectedCountry = country;
  89. countrySlider.value = valueDict[selectedCountry];
  90. changeCountryText();
  91. };
  92. private function polygonsLoaded(event:Event):void {
  93. var loader:URLLoader = URLLoader(event.target);
  94. var worldPolygons:Object = JSON.decode(loader.data);
  95. for each(var country:Object in worldPolygons) {
  96. var geometryWithValue:MultiPolygonWithValue =
  97. new MultiPolygonWithValue();
  98. geometryWithValue.id = country.name;
  99. geometryWithValue.addPolygonFromEncoded(
  100. country.coordinates,
  101. new PolygonOptions({
  102. strokeStyle: new StrokeStyle({
  103. alpha: 0
  104. }),
  105. fillStyle: new FillStyle({
  106. color: 0x0000ff,
  107. alpha: 0.7})
  108. }));
  109. geometryWithValue.addEventListener(
  110. MapMouseEvent.CLICK, curry(countryClicked, country.name));
  111. geometries[country.name] = geometryWithValue;
  112. }
  113. var loader2:URLLoader = new URLLoader();
  114. loader2.addEventListener(Event.COMPLETE, dataLoaded);
  115. loader2.load(new URLRequest("2005_per_capita_emissions.js"));
  116. }
  117. private function onMapReady(event:Event):void {
  118. map.disableContinuousZoom();
  119. map.enableScrollWheelZoom(); //can't make it work on Mac OS X
  120. map.addControl(new PositionControl());
  121. map.addControl(new ZoomControl());
  122. var loader:URLLoader = new URLLoader();
  123. loader.addEventListener(Event.COMPLETE, polygonsLoaded);
  124. loader.load(new URLRequest("world_polygons.encjs"));
  125. map.setCenter(new LatLng(30, 20), 2, MapType.NORMAL_MAP_TYPE);
  126. }
  127. private function changeCountryText():void {
  128. var rounded:Number = Math.round(countrySlider.value * 100) / 100.0;
  129. countryLabel.htmlText = selectedCountry + ": " + rounded.toString();
  130. }
  131. private function countrySliderChanged():void {
  132. geometries[selectedCountry].amount = countrySlider.value;
  133. changeCountryText();
  134. thematicOverlay.draw();
  135. }
  136. private function borderSlider1Changed():void {
  137. borderSliderChanged(1, borderSlider1.value);
  138. }
  139. private function borderSlider2Changed():void {
  140. borderSliderChanged(2, borderSlider2.value);
  141. }
  142. private function borderSliderChanged(index:int, value:Number):void {
  143. borders[index] = value;
  144. createGradients();
  145. thematicOverlay.gradientRule = ruleList;
  146. thematicOverlay.draw();
  147. }
  148. ]]>
  149. </mx:Script>
  150. <mx:HBox
  151. paddingLeft="20"
  152. paddingTop="20"
  153. width="100%"
  154. height="100%"
  155. horizontalGap="0"
  156. verticalGap="30"
  157. backgroundColor="#ffffff">
  158. <mx:VBox
  159. width="900"
  160. height="100%"
  161. horizontalAlign="center"
  162. horizontalGap="10"
  163. verticalGap="0"
  164. backgroundColor="#ffffff">
  165. <mx:Text
  166. color="#666666"
  167. fontSize="20"
  168. text="Gradient Control Demo: 2005 Greenhouse Gas Emissions per capita"
  169. />
  170. <maps:Map
  171. xmlns:maps="com.google.maps.*"
  172. id="map" mapevent_mapready="onMapReady(event)"
  173. width="900"
  174. height="600"
  175. />
  176. <gvlt:GradientBar width="900" height="15" id="gradientBar"/>
  177. <mx:Label
  178. color="#666666"
  179. fontSize="20"
  180. textAlign="center"
  181. htmlText="Select transition point from green to yellow"/>
  182. <mx:HSlider
  183. width="900"
  184. height="50"
  185. id="borderSlider1"
  186. liveDragging="true"
  187. maximum="50"
  188. minimum="0"
  189. showDataTip="true"
  190. tickInterval="10"
  191. labels = "[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50]"
  192. enabled = "false"
  193. change="borderSlider1Changed()"
  194. />
  195. <mx:Label
  196. color="#666666"
  197. fontSize="20"
  198. textAlign="center"
  199. htmlText="Select transition point from orange to red"/>
  200. <mx:HSlider
  201. width="900"
  202. height="50"
  203. id="borderSlider2"
  204. liveDragging="true"
  205. maximum="50"
  206. minimum="0"
  207. showDataTip="true"
  208. tickInterval="10"
  209. labels = "[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50]"
  210. enabled = "false"
  211. change="borderSlider2Changed()"
  212. />
  213. <mx:Text
  214. color="#666666"
  215. fontSize="15">
  216. <mx:htmlText>
  217. <![CDATA[Source: <a href="http://cait.wri.org/cait.php?page=yearly&mode=view&sort=val-desc&pHints=open&url=form&year=2005&sector=natl&co2=1&ch4=1&n2o=1&pfc=1&hfc=1&sf6=1&update=Update">World Resources Institute</a>]]>
  218. </mx:htmlText>
  219. </mx:Text>
  220. </mx:VBox>
  221. <mx:VBox
  222. paddingLeft="20"
  223. horizontalAlign="center"
  224. verticalAlign="top"
  225. >
  226. <mx:Text
  227. color="#666666"
  228. fontSize="20">
  229. <mx:htmlText>
  230. <![CDATA[Click on a country <br>and move the slider <br>to change its value.]]>
  231. </mx:htmlText>
  232. </mx:Text>
  233. <mx:Label
  234. id="countryLabel"
  235. color="#666666"
  236. fontSize="20"
  237. htmlText="Russia"/>
  238. <mx:VSlider
  239. width="50"
  240. height="600"
  241. id="countrySlider"
  242. liveDragging="true"
  243. maximum="100"
  244. minimum="0"
  245. showDataTip="true"
  246. tickInterval="10"
  247. labels = "[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]"
  248. enabled = "false"
  249. change="countrySliderChanged()"
  250. />
  251. <mx:Label
  252. id="unitsLabel"
  253. color="#666666"
  254. fontSize="10"
  255. text="tons CO2 per capita"/>
  256. </mx:VBox>
  257. </mx:HBox>
  258. </mx:Application>