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