refactoring and some bugfixes
[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 var _lat:Number;
17                 private var _lon:Number;
18                 private var _scale:Number;
19                 private var offset_lat:Number=0;
20                 private var offset_lon:Number=0;
21                 private var inited:Boolean;
22                 private var enabled:Boolean;
23
24                 public function Yahoo(w:Number, h:Number, map:Map) {
25                         super();
26                         this.init(token, w, h);  
27                         this.mapType="satellite";
28                         this.alpha=0.5;
29                         this.map=map;
30                         inited=false;
31                         visible=enabled=false;
32                         this.addEventListener(YahooMapEvent.MAP_INITIALIZE, initHandler);
33                 }
34                 
35                 public function show():void {
36                         visible=enabled=true;
37                         if (inited) { 
38                                 moveto(map.centre_lat, map.centre_lon, map.scale);
39                                 this.setSize(map.mapwidth,map.mapheight);
40                         }
41
42                         map.addEventListener(MapEvent.MOVE, moveHandler);
43                         map.addEventListener(MapEvent.RESIZE, resizeHandler);
44                         map.addEventListener(MapEvent.NUDGE_BACKGROUND, nudgeHandler);
45                 }
46
47                 public function hide():void {
48                         visible=enabled=false;
49
50                         map.removeEventListener(MapEvent.MOVE, moveHandler);
51                         map.removeEventListener(MapEvent.RESIZE, resizeHandler);
52                         map.removeEventListener(MapEvent.NUDGE_BACKGROUND, nudgeHandler);
53                 }
54                 
55                 private function initHandler(event:YahooMapEvent):void {
56                         inited=true;
57                         if (map.centre_lat) { moveto(map.centre_lat, map.centre_lon, map.scale); }
58                         this.removeEventListener(YahooMapEvent.MAP_INITIALIZE, initHandler);
59                         visible=enabled;
60                 }
61
62                 private function moveHandler(event:MapEvent):void {
63                         if (!inited) { return; }
64                         moveto(event.params.lat, event.params.lon, event.params.scale);
65                 }
66
67                 private function moveto(lat:Number,lon:Number,scale:uint):void {
68                         if (scale>MAXZOOM) { visible=false; return; }
69                         _lat=lat; _lon=lon; _scale=scale;
70                         
71                         visible=true;
72                         this.zoomLevel=18-scale;
73                         this.centerLatLon=new LatLon(lat+offset_lat, lon+offset_lon);
74                 }
75                 
76                 private function resizeHandler(event:MapEvent):void {
77                         moveto(map.centre_lat, map.centre_lon, map.scale);
78                         this.setSize(event.params.width, event.params.height);
79                 }
80                 
81                 private function nudgeHandler(event:MapEvent):void {
82                         var cx:Number=map.lon2coord(map.centre_lon);
83                         var cy:Number=map.lat2coord(map.centre_lat);
84                         offset_lon+=map.coord2lon(cx-event.params.x)-map.centre_lon;
85                         offset_lat+=map.coord2lat(cy-event.params.y)-map.centre_lat;
86                         moveto(_lat,_lon,_scale);
87                 }
88         }
89 }