| 1 | package gyuque.gmap
|
|---|
| 2 | {
|
|---|
| 3 | import flash.display.*;
|
|---|
| 4 | import flash.geom.Point;
|
|---|
| 5 | import flash.text.TextField;
|
|---|
| 6 | import flash.text.TextFormat;
|
|---|
| 7 | import flash.events.MouseEvent;
|
|---|
| 8 | import gyuque.gmap.googlemaps.*;
|
|---|
| 9 |
|
|---|
| 10 | public class GMapView extends MovieClip implements IDebugOut
|
|---|
| 11 | {
|
|---|
| 12 | private static const GOOGLE_LOGO_URL:String = "http://www.google.com/intl/ja_jp/mapfiles/poweredby.png";
|
|---|
| 13 |
|
|---|
| 14 | private var txDebugOut:TextField = null;
|
|---|
| 15 | private var fmtDebugOut:TextFormat;
|
|---|
| 16 | private var mDrag:DragInfo = new DragInfo();
|
|---|
| 17 | private var mBaseSprite:Sprite;
|
|---|
| 18 | private var mSuperLayer:LayerManager;
|
|---|
| 19 | protected var mCurrentViewport:GMapViewport;
|
|---|
| 20 |
|
|---|
| 21 | public function GMapView(options:*)
|
|---|
| 22 | {
|
|---|
| 23 | mBaseSprite = putBaseSprite();
|
|---|
| 24 | if (options.initial_size)
|
|---|
| 25 | {
|
|---|
| 26 | clearBase(options.initial_size[0], options.initial_size[1]);
|
|---|
| 27 |
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | mSuperLayer = new LayerManager(this);
|
|---|
| 31 | addChild(mSuperLayer);
|
|---|
| 32 |
|
|---|
| 33 | if (options.debug_box)
|
|---|
| 34 | {
|
|---|
| 35 | txDebugOut = new TextField();
|
|---|
| 36 | addChild(txDebugOut);
|
|---|
| 37 |
|
|---|
| 38 | txDebugOut.selectable = false;
|
|---|
| 39 | fmtDebugOut = new TextFormat();
|
|---|
| 40 | fmtDebugOut.size = 9;
|
|---|
| 41 | txDebugOut.y = 90;
|
|---|
| 42 | txDebugOut.height = height-90;
|
|---|
| 43 | txDebugOut.width = 600;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | addGoogleMapLayer();
|
|---|
| 47 |
|
|---|
| 48 | if (options.initial_viewport)
|
|---|
| 49 | {
|
|---|
| 50 | setViewport(options.initial_viewport);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | hookStdEvents();
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | protected function setViewport(v:GMapViewport):void
|
|---|
| 57 | {
|
|---|
| 58 | mCurrentViewport = v;
|
|---|
| 59 | fireViewportChange();
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | protected function fireViewportChange(dx:Number = 0, dy:Number = 0, dzm:int = 0):void
|
|---|
| 63 | {
|
|---|
| 64 | var e:GMapViewEvent = new GMapViewEvent(mCurrentViewport, GMapViewEvent.VIEWPORT_CHANGED);
|
|---|
| 65 | e.screenDX = dx;
|
|---|
| 66 | e.screenDY = dy;
|
|---|
| 67 | e.dZoom = dzm;
|
|---|
| 68 | dispatchEvent(e);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | protected function putBaseSprite():Sprite
|
|---|
| 72 | {
|
|---|
| 73 | var s:Sprite = new Sprite();
|
|---|
| 74 | addChild(s);
|
|---|
| 75 |
|
|---|
| 76 | return s;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | protected function addGoogleMapLayer():Boolean
|
|---|
| 80 | {
|
|---|
| 81 | var lyr:GMapMapLayer = new GMapMapLayer(this, mSuperLayer);
|
|---|
| 82 | lyr.debug_out = this;
|
|---|
| 83 | mSuperLayer.push(lyr);
|
|---|
| 84 |
|
|---|
| 85 | return true;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | protected function clearBase(w:int, h:int):void
|
|---|
| 89 | {
|
|---|
| 90 | var g:Graphics = mBaseSprite.graphics;
|
|---|
| 91 | g.beginFill(0xe5e0d5);
|
|---|
| 92 | g.drawRect(0,0, w, h);
|
|---|
| 93 | g.endFill();
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | protected function hookStdEvents():void
|
|---|
| 97 | {
|
|---|
| 98 | addEventListener(MouseEvent.MOUSE_MOVE , onMouseMove);
|
|---|
| 99 | addEventListener(MouseEvent.MOUSE_DOWN , onMouseDown);
|
|---|
| 100 | addEventListener(MouseEvent.MOUSE_UP , onMouseUp);
|
|---|
| 101 | addEventListener(MouseEvent.MOUSE_OUT , onMouseOut);
|
|---|
| 102 | addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | protected function onMouseWheel(e:MouseEvent):void
|
|---|
| 106 | {
|
|---|
| 107 | zoom((e.delta > 0) ? 1 : -1, e.stageX, e.stageY);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | protected function zoom(d:int, px:Number = -1, py:Number = -1):void
|
|---|
| 111 | {
|
|---|
| 112 | var z:int = mCurrentViewport.zoom;
|
|---|
| 113 | var oldz:int = z;
|
|---|
| 114 | z += d;
|
|---|
| 115 | if (z < 0) z = 0;
|
|---|
| 116 | if (z > 16) z = 16;
|
|---|
| 117 |
|
|---|
| 118 | mCurrentViewport.zoom = z;
|
|---|
| 119 | if (px >= 0 || py >= 0) {
|
|---|
| 120 | var mv:Point = calcFixedPointZoom(px, py, (d>0) ? 2 : 0.5, mCurrentViewport);
|
|---|
| 121 | mCurrentViewport.moveByPixel(mv.x, mv.y);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | fireViewportChange(0,0,z - oldz);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | protected function calcFixedPointZoom(sx:Number, sy:Number, zoomRatio:Number, vpAfter:GMapViewport):Point
|
|---|
| 128 | {
|
|---|
| 129 | sx -= vpAfter.width *0.5;
|
|---|
| 130 | sy -= vpAfter.height*0.5;
|
|---|
| 131 |
|
|---|
| 132 | var sx2:Number = sx * zoomRatio;
|
|---|
| 133 | var sy2:Number = sy * zoomRatio;
|
|---|
| 134 |
|
|---|
| 135 | var dx:Number = sx2 - sx;
|
|---|
| 136 | var dy:Number = sy2 - sy;
|
|---|
| 137 |
|
|---|
| 138 | // var retval:Point = new Point();
|
|---|
| 139 | // vpAfter.transformVectorScreenToView(dx, dy, retval);
|
|---|
| 140 | // return retval;
|
|---|
| 141 | return new Point(dx, dy);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | protected function onMouseOut(e:MouseEvent):void
|
|---|
| 145 | {
|
|---|
| 146 | if (e.stageX < 0 || e.stageY < 0 ||
|
|---|
| 147 | e.stageX >= width || e.stageY >= height)
|
|---|
| 148 | mDrag.dragging = false;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | protected function onMouseMove(e:MouseEvent):void
|
|---|
| 152 | {
|
|---|
| 153 | if (mDrag.dragging)
|
|---|
| 154 | {
|
|---|
| 155 | mDrag.update(e.stageX, e.stageY);
|
|---|
| 156 |
|
|---|
| 157 | cls();
|
|---|
| 158 | puts((mCurrentViewport.lat/0.0174533)+" "+(mCurrentViewport.lng/0.0174533));
|
|---|
| 159 | moveByPixel(-mDrag.dx, -mDrag.dy);
|
|---|
| 160 | puts(e.stageX.toString()+","+e.stageY.toString()+" "+mDrag.dx.toString()+","+mDrag.dy.toString());
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | protected function onMouseDown(e:MouseEvent):void
|
|---|
| 165 | {
|
|---|
| 166 | mDrag.dragging = true;
|
|---|
| 167 | mDrag.update(e.stageX, e.stageY);
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | protected function onMouseUp(e:MouseEvent):void
|
|---|
| 171 | {
|
|---|
| 172 | mDrag.dragging = false;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | public function moveByPixel(dx:Number, dy:Number):void
|
|---|
| 176 | {
|
|---|
| 177 | if (mCurrentViewport)
|
|---|
| 178 | {
|
|---|
| 179 | mCurrentViewport.moveByPixel(dx, dy);
|
|---|
| 180 | fireViewportChange(dx, dy);
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | public function puts(s:String):void
|
|---|
| 185 | {
|
|---|
| 186 | if (!txDebugOut)
|
|---|
| 187 | return;
|
|---|
| 188 |
|
|---|
| 189 | txDebugOut.appendText(s);
|
|---|
| 190 | txDebugOut.appendText("\r\n");
|
|---|
| 191 | txDebugOut.setTextFormat(fmtDebugOut);
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | public function cls():void
|
|---|
| 195 | {
|
|---|
| 196 | txDebugOut.text = "";
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | public function d_outMetrics():void
|
|---|
| 200 | {
|
|---|
| 201 | puts("w: "+ width.toString());
|
|---|
| 202 | puts("h: "+ height.toString());
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | class DragInfo
|
|---|
| 208 | {
|
|---|
| 209 | public function DragInfo (){dragging=false;}
|
|---|
| 210 | public var prevX:Number;
|
|---|
| 211 | public var prevY:Number;
|
|---|
| 212 | public var dx:Number;
|
|---|
| 213 | public var dy:Number;
|
|---|
| 214 | public var dragging:Boolean;
|
|---|
| 215 |
|
|---|
| 216 | public function update(x:Number, y:Number):void
|
|---|
| 217 | {
|
|---|
| 218 | dx = x - prevX;
|
|---|
| 219 | dy = y - prevY;
|
|---|
| 220 |
|
|---|
| 221 | prevX = x;
|
|---|
| 222 | prevY = y;
|
|---|
| 223 | }
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|