May 26, 2008

Google Earth control in Google Maps

I placed Google Earth control in Google Maps.

Here is the code(185 lines):

  1. package {
  2.     import flash.display.*;
  3.     import flash.geom.*;
  4.     import flash.events.*;
  5.     import flash.filters.DisplacementMapFilter;
  6.     import com.google.maps.*;
  7.  
  8.     [SWF(backgroundColor="0x000000")]
  9.     public class GoogleEarthControl extends Sprite {
  10.         private const WIDTH:int = 400;
  11.         private const HEIGHT:int = 400;
  12.         private const VIEWDISTANCE:Number = 500;
  13.         private var map:Map;
  14.         private var mapContainer:Sprite;
  15.         private var bmd:BitmapData;
  16.  
  17.         public function GoogleEarthControl() {
  18.             stage.scaleMode = "noScale";
  19.             stage.align = "TL";
  20.  
  21.             mapContainer = new Sprite();
  22.             addChild(mapContainer);
  23.  
  24.             map = new Map();
  25.             map.key = "ABQIAAAA6de2NwhEAYfH7t7oAYcX3xRWPxFShKMZYAUclLzloAj2mNQgoRQZnk8BRyG0g_m2di3bWaT-Ji54Lg";
  26.             map.setSize(new Point(WIDTH, HEIGHT));
  27.             map.addEventListener(MapEvent.MAP_READY, function(event:*):void{
  28.                 map.setCenter(new LatLng(35.003759, 135.769322), 10, MapType.SATELLITE_MAP_TYPE);
  29.  
  30.                 var mapMask:Sprite = new Sprite();
  31.                 mapMask.graphics.beginFill(0);
  32.                 mapMask.graphics.drawRect(0, 0, WIDTH, HEIGHT);
  33.                 mapMask.graphics.endFill();
  34.                 mapContainer.mask = mapMask;
  35.  
  36.                 bmd = new BitmapData(WIDTH, HEIGHT);
  37.  
  38.                 var s1:ScrollBar = new ScrollBar(50);
  39.                 addChild(s1);
  40.                 s1.rotation = -90
  41.                 s1.x = WIDTH - 100; s1.y = 30;
  42.                 s1.addEventListener("change", function(event:Event):void{
  43.                     updateValue(s1.value);
  44.                 });
  45.                 s1.dispatchEvent(new Event("change"));
  46.  
  47.                 var s2:ScrollBar = new ScrollBar(80);
  48.                 addChild(s2);
  49.                 s2.x = WIDTH - 10; s2.y = 50;
  50.                 s2.addEventListener("change", function(event:Event):void{
  51.                     map.setZoom((100 - s2.value) / 100 * map.getMaxZoomLevel());
  52.                 });
  53.                 s2.dispatchEvent(new Event("change"));
  54.  
  55.                 var r:RotationControl = new RotationControl(-10);
  56.                 addChild(r);
  57.                 r.x = WIDTH - 60; r.y = 90;
  58.                 r.addEventListener("change", function(event:Event):void{
  59.                     var matrix:Matrix = new Matrix();
  60.                     matrix.translate(-WIDTH / 2, -HEIGHT / 2);
  61.                     matrix.rotate(r.value * Math.PI / 180);
  62.                     matrix.translate(WIDTH / 2, HEIGHT / 2);
  63.                     map.transform.matrix = matrix;
  64.                 });
  65.                 r.dispatchEvent(new Event("change"));
  66.             });
  67.             mapContainer.addChild(map);
  68.         }
  69.  
  70.         private function updateValue(value:int):void{
  71.             var rad:Number = value / 100 * 60 * Math.PI / 180;
  72.             var p:Number = -Math.sin(rad) / VIEWDISTANCE;
  73.  
  74.             bmd.lock();
  75.             var HW:int = WIDTH / 2;
  76.             var HH:int = HEIGHT / 2;
  77.             for(var j:int = -HH; j < HH; j++){
  78.                 var pj:Number = 1 + j * p;
  79.                 for(var i:int = -HW; i < HW; i++){
  80.                     var _x:int = pj * i;
  81.                     var _y:int = pj * j / Math.cos(rad);
  82.                     bmd.setPixel(i + HW, j + HH, getColor((_x - i) * 1 + 0x80, (_y - j) * 1 + 0x80, 0));
  83.                 }
  84.             }
  85.             bmd.unlock();
  86.  
  87.             mapContainer.filters = [new DisplacementMapFilter(bmd, new Point(0, 0), 1, 2, 256, 256, "color")];
  88.         }
  89.  
  90.         private static function bounds(val:Number, min:Number = Number.POSITIVE_INFINITY, max:Number = Number.NEGATIVE_INFINITY):Number {
  91.             return Math.max(Math.min(val, max), min);
  92.         }
  93.  
  94.         private static function getColor(r:int, g:int, b:int):uint {
  95.             return Math.floor(bounds(r, 0, 255)) * 0x10000
  96.                  + Math.floor(bounds(g, 0, 255)) * 0x100
  97.                  + Math.floor(bounds(b, 0, 255));
  98.         }
  99.     }
  100. }
  101.  
  102. import flash.display.Sprite;
  103. import flash.events.*;
  104. import flash.geom.Point;
  105.  
  106. class ScrollBar extends Sprite {
  107.     public var value:int;
  108.  
  109.     public function ScrollBar(_value:int):void {
  110.         value = _value;
  111.  
  112.         useHandCursor = buttonMode = true;
  113.         graphics.beginFill(0xffffff);
  114.         graphics.lineStyle(0);
  115.         graphics.drawRect(0, -2, 8, 112);
  116.         graphics.endFill();
  117.  
  118.         var tab:Sprite = new Sprite();
  119.         tab.graphics.beginFill(0xffffff);
  120.         tab.graphics.lineStyle(0);
  121.         tab.graphics.drawRect(-8, 0, 24, 8);
  122.         tab.graphics.endFill();
  123.         tab.y = _value;
  124.         addChild(tab);
  125.  
  126.         addEventListener("mouseDown", function(event:MouseEvent):void {
  127.             stage.addEventListener("mouseMove", mouseMoveHandler);
  128.             stage.addEventListener("mouseUp", mouseUpHandler);
  129.             mouseMoveHandler(event);
  130.         });
  131.  
  132.         var mouseMoveHandler:Function = function(event:MouseEvent):void {
  133.             var p:Point = globalToLocal(new Point(stage.mouseX, stage.mouseY));
  134.             tab.y = Math.min(Math.max(0, p.y), 100);
  135.         }
  136.         var mouseUpHandler:Function = function(event:MouseEvent):void {
  137.             value = tab.y;
  138.             dispatchEvent(new Event("change"));
  139.             stage.removeEventListener("mouseMove", mouseMoveHandler);
  140.             stage.removeEventListener("mouseUp", mouseUpHandler);
  141.         }
  142.     }
  143. }
  144.  
  145. class RotationControl extends Sprite {
  146.     public var value:int = 0;
  147.  
  148.     public function RotationControl(_value:int):void {
  149.         value = _value;
  150.  
  151.         useHandCursor = buttonMode = true;
  152.         graphics.beginFill(0xffffff);
  153.         graphics.lineStyle(0);
  154.         graphics.drawCircle(0, 0, 40);
  155.         graphics.drawCircle(0, 0, 32);
  156.         graphics.endFill();
  157.  
  158.         var tab:Sprite = new Sprite();
  159.         tab.graphics.beginFill(0xffffff);
  160.         tab.graphics.lineStyle(0);
  161.         tab.graphics.drawRect(-44, -8, 16, 16);
  162.         tab.graphics.endFill();
  163.         tab.rotation = _value + 90;
  164.         addChild(tab);
  165.  
  166.         addEventListener("mouseDown", function(event:MouseEvent):void {
  167.             stage.addEventListener("mouseMove", mouseMoveHandler);
  168.             stage.addEventListener("mouseUp", mouseUpHandler);
  169.             mouseMoveHandler(event);
  170.         });
  171.  
  172.         var mouseMoveHandler:Function = function(event:MouseEvent):void {
  173.             var p:Point = globalToLocal(new Point(stage.mouseX, stage.mouseY));
  174.             tab.rotation = p.x == 0 ? 90 : Math.atan(p.y / p.x) / Math.PI * 180 + (p.x > 0 ? 180 : 0);
  175.             value = tab.rotation - 90;
  176.             dispatchEvent(new Event("change"));
  177.         }
  178.         var mouseUpHandler:Function = function(event:MouseEvent):void {
  179.             value = tab.rotation - 90;
  180.             dispatchEvent(new Event("change"));
  181.             stage.removeEventListener("mouseMove", mouseMoveHandler);
  182.             stage.removeEventListener("mouseUp", mouseUpHandler);
  183.         }
  184.     }
  185. }