make Yahoo code less horrid, and fix the overzooming bug
[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                 public function Yahoo(w:Number, h:Number, map:Map) {
17                         super();
18                         this.init(token, w, h);  
19                         this.mapType="satellite";
20                         this.alpha=0.5;
21                         this.map=map;
22                 }
23                 
24                 public function show():void {
25                         this.visible=true;
26                         this.zoomLevel = 18-map.scale;
27                         this.centerLatLon = new LatLon(map.centre_lat, map.centre_lon);
28
29                         this.addEventListener(YahooMapEvent.MAP_INITIALIZE, initHandler);
30                         map.addEventListener(MapEvent.MOVE, moveHandler);
31                         map.addEventListener(MapEvent.RESIZE, resizeHandler);
32                 }
33
34                 public function hide():void {
35                         this.visible=false;
36
37                         this.removeEventListener(YahooMapEvent.MAP_INITIALIZE, initHandler);
38                         map.removeEventListener(MapEvent.MOVE, moveHandler);
39                         map.removeEventListener(MapEvent.RESIZE, resizeHandler);
40                 }
41                 
42                 private function initHandler(event:YahooMapEvent):void {
43                         moveto(map.centre_lat, map.centre_lon, map.scale);
44                 }
45
46                 private function moveHandler(event:MapEvent):void {
47                         moveto(event.params.lat, event.params.lon, event.params.scale);
48                 }
49
50                 private function moveto(lat:Number,lon:Number,scale:uint):void {
51                         if (scale>MAXZOOM) { this.visible=false; return; }
52                         this.visible=true;
53                         this.zoomLevel=18-scale;
54                         this.centerLatLon=new LatLon(lat, lon);
55                 }
56
57                 private function resizeHandler(event:MapEvent):void {
58                         this.setSize(event.params.width, event.params.height);
59                 }
60         }
61 }