PageRenderTime 176ms CodeModel.GetById 162ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/google/maps/extras/gradients/ThematicOverlay.as

http://gmaps-utility-library-flash.googlecode.com/
ActionScript | 75 lines | 41 code | 11 blank | 23 comment | 5 complexity | 797d2c0240d453f428ab5e3057ed8237 MD5 | raw file
 1/**
 2* Copyright 2009 Google Inc. 
 3* Licensed under the Apache License, Version 2.0:
 4*  http://www.apache.org/licenses/LICENSE-2.0
 5*/
 6
 7package com.google.maps.extras.gradients {
 8  import com.google.maps.Color;
 9  import com.google.maps.Map;
10  import com.google.maps.MapMouseEvent;
11  import com.google.maps.overlays.Polygon;
12  import com.google.maps.overlays.PolygonOptions;
13
14  /**
15  * ThematicOverlay is a convenience class for drawing thematic maps.
16  *
17  * @author Simon Ilyushchenko
18  */
19
20
21  public class ThematicOverlay {
22    // Default transparency factor to use when drawing geometries.
23    public var alpha: Number = 0.7;
24    // Gradient rule to use for calculating colors.
25    public var gradientRule:GradientRule;
26
27    private var map:Map;
28    private var polygonsWithValues:Object;
29
30    /** 
31    * Class constructor
32    *
33    * @param map A Flash API Map object
34    * @param gradientRule The gradient rule or list of rules to use
35    * @param polygonsWithValues The list of MultiPolygonWithValues to draw
36    */  
37    public function ThematicOverlay(
38          map:Map, gradientRule:GradientRule, polygonsWithValues:Object) {
39      this.map = map;
40      this.gradientRule = gradientRule;
41      this.polygonsWithValues = polygonsWithValues;
42
43      for each(var p:MultiPolygonWithValue in this.polygonsWithValues) {
44        p.addToMap(map); 
45      }
46    }
47    
48    /** 
49    * Draw this overlay on the map, coloring all geometries
50    * in appropriate colors
51    */
52    public function draw():void {
53      var values:Array = [];
54      for each(var p:MultiPolygonWithValue in polygonsWithValues) {
55        values.push(p.amount);
56      }
57
58      var gradientControl:GradientControl = 
59          gradientRule.applyGradientToValueList(values);
60
61      for each(p in polygonsWithValues) {
62        var options:Object = {};
63        if (isNaN(p.amount)) {
64          options.alpha = 0.0;
65        } else {
66          var color:Number = gradientControl.colorForValue(p.amount);
67          options.color = color;
68          options.alpha = alpha;
69        }
70 
71        p.setOptions(new PolygonOptions({fillStyle: options})); 
72      }
73    }
74  }
75}