oh just frigging work
[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) { moveto(map.centre_lat, map.centre_lon, map.scale); }
38
39                         map.addEventListener(MapEvent.MOVE, moveHandler);
40                         map.addEventListener(MapEvent.RESIZE, resizeHandler);
41                         map.addEventListener(MapEvent.NUDGE_BACKGROUND, nudgeHandler);
42                 }
43
44                 public function hide():void {
45                         visible=enabled=false;
46
47                         map.removeEventListener(MapEvent.MOVE, moveHandler);
48                         map.removeEventListener(MapEvent.RESIZE, resizeHandler);
49                         map.removeEventListener(MapEvent.NUDGE_BACKGROUND, nudgeHandler);
50                 }
51                 
52                 private function initHandler(event:YahooMapEvent):void {
53                         inited=true;
54                         if (map.centre_lat) { moveto(map.centre_lat, map.centre_lon, map.scale); }
55                         this.removeEventListener(YahooMapEvent.MAP_INITIALIZE, initHandler);
56                         visible=enabled;
57                 }
58
59                 private function moveHandler(event:MapEvent):void {
60                         if (!inited) { return; }
61                         moveto(event.params.lat, event.params.lon, event.params.scale);
62                 }
63
64                 private function moveto(lat:Number,lon:Number,scale:uint):void {
65                         if (scale>MAXZOOM) { visible=false; return; }
66                         _lat=lat; _lon=lon; _scale=scale;
67                         
68                         visible=true;
69                         this.zoomLevel=18-scale;
70                         this.centerLatLon=new LatLon(lat+offset_lat, lon+offset_lon);
71                 }
72
73                 private function resizeHandler(event:MapEvent):void {
74                         this.setSize(event.params.width, event.params.height);
75                 }
76                 
77                 private function nudgeHandler(event:MapEvent):void {
78                         var cx:Number=map.lon2coord(map.centre_lon);
79                         var cy:Number=map.lat2coord(map.centre_lat);
80                         offset_lon+=map.coord2lon(cx-event.params.x)-map.centre_lon;
81                         offset_lat+=map.coord2lat(cy-event.params.y)-map.centre_lat;
82                         moveto(_lat,_lon,_scale);
83                 }
84         }
85 }