7530324354ac39d61c03ea7c6cfbe870f41d30de
[potlatch2.git] / net / systemeD / potlatch2 / Yahoo.as
1 package net.systemeD.potlatch2 {
2
3         import flash.display.*;
4         import net.systemeD.halcyon.Map;
5         import net.systemeD.halcyon.MapEvent;
6         import com.yahoo.maps.api.YahooMap;
7         import com.yahoo.maps.api.YahooMapEvent;
8         import com.yahoo.maps.api.core.location.LatLon;
9         
10     public class Yahoo extends YahooMap {
11
12                 private var map:Map;
13                 private static const token:String="f0a.sejV34HnhgIbNSmVHmndXFpijgGeun0fSIMG9428hW_ifF3pYKwbV6r9iaXojl1lU_dakekR";
14                 private static const MAXZOOM:int=17;
15
16                 private static const UNINITIALISED:uint=0;
17                 private static const INITIALISING:uint=1;
18                 private static const HIDDEN:uint=2;
19                 private static const SHOWING:uint=3;
20                 private var currentState:uint=UNINITIALISED;
21
22                 private var _lat:Number;
23                 private var _lon:Number;
24                 private var _scale:Number;
25                 private var offset_lat:Number=0;
26                 private var offset_lon:Number=0;
27
28                 public function Yahoo(map:Map) {
29                         super();
30                         currentState=UNINITIALISED;
31                         this.map=map;
32                         visible=false;
33                 }
34                 
35                 public function show():void {
36                         visible=true;
37                         if (currentState==UNINITIALISED) {
38                                 currentState=INITIALISING;
39                                 this.addEventListener(YahooMapEvent.MAP_INITIALIZE, initHandler);
40                                 this.init(token, map.mapwidth, map.mapheight);
41                                 this.mapType="satellite";
42                                 this.alpha=0.5;                         // ** FIXME - should take the value the user has chosen
43                                 activateListeners();
44                         } else if (currentState==HIDDEN) { 
45                                 currentState=SHOWING;
46                                 moveto(map.centre_lat, map.centre_lon, map.scale);
47                                 this.setSize(map.mapwidth,map.mapheight);
48                                 activateListeners();
49                         }
50                 }
51
52                 public function hide():void {
53                         deactivateListeners();
54                         visible=false;
55                         if (currentState==SHOWING) currentState=HIDDEN;
56                 }
57
58                 private function activateListeners():void {
59                         map.addEventListener(MapEvent.MOVE, moveHandler);
60                         map.addEventListener(MapEvent.RESIZE, resizeHandler);
61                         map.addEventListener(MapEvent.NUDGE_BACKGROUND, nudgeHandler);
62                 }
63                 
64                 private function deactivateListeners():void {
65                         map.removeEventListener(MapEvent.MOVE, moveHandler);
66                         map.removeEventListener(MapEvent.RESIZE, resizeHandler);
67                         map.removeEventListener(MapEvent.NUDGE_BACKGROUND, nudgeHandler);
68                 }
69                 
70                 private function initHandler(event:YahooMapEvent):void {
71                         currentState=visible ? SHOWING : HIDDEN;
72                         if (map.centre_lat) { moveto(map.centre_lat, map.centre_lon, map.scale); }
73                         this.removeEventListener(YahooMapEvent.MAP_INITIALIZE, initHandler);
74                 }
75
76                 private function moveHandler(event:MapEvent):void {
77                         if (currentState!=SHOWING) { return; }
78                         moveto(event.params.lat, event.params.lon, event.params.scale);
79                 }
80
81                 private function moveto(lat:Number,lon:Number,scale:uint):void {
82                         if (scale>MAXZOOM) { visible=false; return; }
83                         _lat=lat; _lon=lon; _scale=scale;
84                         
85                         visible=true;
86                         this.zoomLevel=18-scale;
87                         this.centerLatLon=new LatLon(lat+offset_lat, lon+offset_lon);
88                 }
89                 
90                 private function resizeHandler(event:MapEvent):void {
91                         moveto(map.centre_lat, map.centre_lon, map.scale);
92                         this.setSize(event.params.width, event.params.height);
93                 }
94                 
95                 private function nudgeHandler(event:MapEvent):void {
96                         var cx:Number=map.lon2coord(map.centre_lon);
97                         var cy:Number=map.lat2coord(map.centre_lat);
98                         offset_lon+=map.coord2lon(cx-event.params.x)-map.centre_lon;
99                         offset_lat+=map.coord2lat(cy-event.params.y)-map.centre_lat;
100                         moveto(_lat,_lon,_scale);
101                 }
102         }
103 }