OpenLayers OpenLayers

Changeset 1387

Show
Ignore:
Timestamp:
08/26/06 16:37:00 (2 years ago)
Author:
crschmidt
Message:

Redo Yahoo class to work under EventPane.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/examples/yahoo.html

    r788 r1387  
    99        } 
    1010    </style> 
     11    <script src="http://api.maps.yahoo.com/ajaxymap?v=3.0&appid=euzuro-openlayers"></script> 
    1112    <script src="../lib/OpenLayers.js"></script> 
    1213    <script type="text/javascript"> 
  • trunk/openlayers/lib/OpenLayers.js

    r1379 r1387  
    7171        "OpenLayers/Layer/Google.js", 
    7272        "OpenLayers/Layer/VirtualEarth.js", 
    73 //        "OpenLayers/Layer/Yahoo.js", 
     73        "OpenLayers/Layer/Yahoo.js", 
    7474        "OpenLayers/Layer/HTTPRequest.js", 
    7575        "OpenLayers/Layer/Grid.js", 
  • trunk/openlayers/lib/OpenLayers/Layer/Yahoo.js

    r1206 r1387  
    33 * text of the license. */ 
    44 
    5 // load Yahoo map control script 
    6 document.write("<script src='http://api.maps.yahoo.com/ajaxymap?v=3.0&appid=euzuro-openlayers'></script>"); 
    7  
    85/** 
    9  * @class 
     6 * @class  
    107 *  
    11  * @requires OpenLayers/Layer.js 
     8 * @requires OpenLayers/Layer/EventPane.js 
    129 */ 
    1310OpenLayers.Layer.Yahoo = Class.create(); 
    14 OpenLayers.Layer.Yahoo.prototype = Object.extend( new OpenLayers.Layer(), { 
    15  
    16     /** Yahoo layer is always a base layer. 
    17      *  
    18      * @type Boolean 
    19      */ 
    20     isBaseLayer: true, 
    21      
    22     /** @type Boolean */ 
    23     isFixed: true, 
    24  
    25     /** @type GMap2 gmap stores the Google Map element */ 
    26     ymap:null, 
    27     
    28     /** @type Boolean */ 
    29     dragging:false, 
    30      
     11OpenLayers.Layer.Yahoo.prototype =  
     12  Object.extend( new OpenLayers.Layer.EventPane(), { 
     13 
     14    /** @type YMap */ 
     15    yahoomap: null, 
     16     
     17    /** @type int */ 
     18    minZoomLevel: 0, 
     19 
     20    /** @type int */ 
     21    maxZoomLevel: 15, 
     22     
     23    /** 
     24     * @constructor 
     25     *  
     26     * @param {String} name 
     27     */ 
     28    initialize:function(name) { 
     29        OpenLayers.Layer.EventPane.prototype.initialize.apply(this, arguments); 
     30 
     31        this.numZoomLevels = this.maxZoomLevel - this.minZoomLevel + 1; 
     32    }, 
     33 
    3134    /**  
    32      * @constructor 
    33      *  
    34      * @param {String} name 
    35      */ 
    36     initialize: function(name) { 
    37         OpenLayers.Layer.prototype.initialize.apply(this, [name]); 
    38     }, 
    39      
    40      /**  
    4135     * @param {OpenLayers.Map} map 
    4236     */ 
    4337    setMap:function(map) { 
    44         OpenLayers.Layer.prototype.setMap.apply(this, arguments); 
    45  
    46         // once our layer has been added to the map, we can create the vemap 
    47         this.map.events.register("addlayer", this, this.loadYMap); 
     38        OpenLayers.Layer.EventPane.prototype.setMap.apply(this, arguments); 
     39 
     40        // once our layer has been added to the map, we can load the yahoomap 
     41        this.loadYMap(); 
    4842    }, 
    4943 
     
    5549    moveTo:function(bounds, zoomChanged, minor) { 
    5650 
    57         if ((this.ymap != null) && (!this.dragging)) { 
    58  
     51        if (this.yahoomap != null) { 
    5952            var olCenter = this.map.getCenter(); 
    60             var yCenter = this.getYMapCenter(); 
     53            var mmCenter = this.getYLatLongFromOLLonLat(olCenter); 
     54     
     55            if (zoomChanged) { 
     56                var olZoom = this.map.getZoom(); 
     57                var mmZoom = this.getYZoomFromOLZoom(olZoom); 
     58                this.yahoomap.setZoomLevel(mmZoom);  
     59            } 
     60            this.yahoomap.drawZoomAndCenter(mmCenter, mmZoom); 
     61     
     62        } 
     63    }, 
     64 
     65 
     66    /** 
     67     *  
     68     */ 
     69    loadYMap:function() { 
     70            this.yahoomap = new YMap(this.div); 
     71 
     72         
     73        if (this.yahoomap == null) { 
     74            this.loadWarningMessage(); 
     75        } 
     76  
     77    }, 
     78 
     79    /** If we can't load the yahoomap, then display an error message to the  
     80     *   user and tell them where to go for help. 
     81     *  
     82     * @private 
     83     *  
     84     */ 
     85    loadWarningMessage:function() { 
     86 
     87        this.div.style.backgroundColor = "darkblue"; 
     88 
     89        var html = ""; 
     90        html += "The Y Layer was unable to load correctly.<br>"; 
     91        html += "<br>"; 
     92        html += "To get rid of this message, click on the Y Layer's " 
     93        html += "tab in the layer switcher in the upper-right corner.<br>"; 
     94        html += "<br>"; 
     95        html += "Most likely, this is because the Y library"; 
     96        html += " script was either not correctly included.<br>"; 
     97        html += "<br>"; 
     98        html += "Demmlopers: For help getting this working correctly, "; 
     99        html += "<a href='http://trac.openlayers.org/wiki/YahooLayer' " 
     100        html +=  "target='_blank'>"; 
     101        html +=     "click here"; 
     102        html += "</a>"; 
     103         
     104        var viewSize = this.map.getSize(); 
     105         
     106        msgW = Math.min(viewSize.w, 300); 
     107        msgH = Math.min(viewSize.h, 200); 
     108        var size = new OpenLayers.Size(msgW, msgH); 
     109 
     110        var centerPx = new OpenLayers.Pixel(viewSize.w/2, viewSize.h/2); 
     111 
     112        var topLeft = centerPx.add(-size.w/2, -size.h/2);             
     113 
     114        var div = OpenLayers.Util.createDiv("mmWarning",  
     115                                            topLeft,  
     116                                            size, 
     117                                            null, 
     118                                            null, 
     119                                            null, 
     120                                            "auto"); 
     121 
     122        div.style.padding = "7px"; 
     123        div.style.backgroundColor = "yellow"; 
     124 
     125        div.innerHTML = html; 
     126        this.div.appendChild(div); 
     127    }, 
     128 
     129 
     130  /********************************************************/ 
     131  /*                                                      */ 
     132  /*                 Baselayer Functions                  */ 
     133  /*                                                      */ 
     134  /********************************************************/ 
     135 
     136    /** 
     137     * @param {OpenLayers.Pixel} viewPortPx 
     138     * 
     139     * @returns An OpenLayers.LonLat which is the passed-in view port 
     140     *          OpenLayers.Pixel, translated into lon/lat by Y 
     141     *          If yahoomap is not loaded, returns null. 
     142     * @type OpenLayers.LonLat 
     143     */ 
     144    getLonLatFromViewPortPx: function (viewPortPx) { 
     145        var lonlat = null; 
     146        if (this.yahoomap != null) { 
     147            var pixel = this.getPixelFromOLPixel(viewPortPx); 
     148            var mmLatLong = this.yahoomap.convertXYLatLon(pixel); 
     149            lonlat = this.getOLLonLatFromYLatLong(mmLatLong); 
     150        } 
     151        return lonlat; 
     152    }, 
     153 
     154  
     155    /** 
     156     * @param {OpenLayers.LonLat} lonlat 
     157     * 
     158     * @returns An OpenLayers.Pixel which is the passed-in OpenLayers.LonLat,  
     159     *          translated into view port pixels BY Y 
     160     *          If yahoomap is not loaded, returns null. 
     161     * @type OpenLayers.Pixel 
     162     */ 
     163    getViewPortPxFromLonLat: function (lonlat) { 
     164        var viewPortPx = null; 
     165        if (this.yahoomap != null) { 
     166            var mmLatLong = this.getYLatLongFromOLLonLat(lonlat); 
     167            var pixel = this.yahoomap.convertLatLonXY(mmLatLong); 
     168            viewPortPx = this.getOLPixelFromPixel(pixel); 
     169        } 
     170        return viewPortPx; 
     171    }, 
     172 
     173 
     174    /** 
     175     * @param {OpenLayers.Bounds} bounds 
     176     * 
     177     * @returns Corresponding zoom lemml for a specified Bounds.  
     178     *          If yahoomap is not loaded, returns null. 
     179     * @type int 
     180     */ 
     181    getZoomForExtent: function (bounds) { 
     182 
     183        var zoom = null; 
     184        if (this.yahoomap != null) { 
     185            var maxRes = this.map.getMaxResolution(); 
     186            var viewSize = this.map.getSize(); 
     187     
     188            var width = bounds.getWidth(); 
     189            var height = bounds.getHeight(); 
     190     
     191            var degPerPixel = (width > height) ? width / viewSize.w  
     192                                               : height / viewSize.h; 
    61193             
    62             var olZoom = this.map.getZoom(); 
    63             var yZoom = this.ymap.getZoomLevel(); 
    64              
    65             if ((!olCenter.equals(yCenter)) || (( 16 - olZoom) != yZoom)) { 
    66                 this.ymap.drawZoomAndCenter(new YGeoPoint(olCenter.lat, olCenter.lon),  
    67                                     16 - olZoom); 
    68             } 
    69         } 
    70     }, 
    71  
    72     /** 
    73      *  
    74      */ 
    75     loadYMap:function() { 
    76         // create div and set to same size as map 
    77         var yDiv = OpenLayers.Util.createDiv(this.name); 
    78         var sz = this.map.getSize(); 
    79         yDiv.style.width = sz.w; 
    80         yDiv.style.height = sz.h; 
    81         this.div.appendChild(yDiv); 
    82  
    83         // create GMap, hide nav controls 
    84         this.ymap = new YMap(this.div); 
    85  
    86         // catch pans and zooms from GMap 
    87         YEvent.Capture(this.ymap,  
    88                        EventsList.endPan,  
    89                        this.catchPanZoom,  
    90                        this);  
    91  
    92         // catch pans and zooms from GMap 
    93         YEvent.Capture(this.ymap,  
    94                        EventsList.endAutoPan,  
    95                        this.catchPanZoom,  
    96                        this);  
    97  
    98  
    99         // attach to the drag start and end and weŽll set a flag so that 
    100         //  we dont get recursivity. this is because the events fall through 
    101         //  the gmaps div and into the main layer div 
    102         YEvent.Capture(this.ymap,  
    103                        EventsList.startPan,  
    104                        this.dragStart, 
    105                        this);  
    106  
    107     }, 
    108  
    109     /**  
    110      * @private 
    111      */ 
    112     dragStart: function() { 
    113         this.dragging = true; 
    114     }, 
    115      
    116     /** 
    117      * @private  
    118      *  
    119      * @param {Event} e 
    120      */ 
    121     catchPanZoom: function(e) {  
    122         this.dragging = false; 
    123  
    124         var olCenter = this.getYMapCenter(); 
    125         var yZoom = this.ymap.getZoomLevel(); 
    126  
    127         this.map.setCenter(olCenter, 16 - yZoom); 
    128          
    129     }, 
    130  
    131     /** 
    132      * @private 
    133      *  
    134      * @returns An OpenLayers.LonLat with the center of the ymap, or null if  
    135      *           the YMap has not been centered yet 
     194            var mmZoom = Math.floor( (Math.log(maxRes/degPerPixel)) /  
     195                                     Math.log(2) ); 
     196     
     197            //make sure zoom is within bounds     
     198            var mmZoom = Math.min(Math.max(mmZoom, this.minZoomLevel),  
     199                                  this.maxZoomLevel); 
     200     
     201            zoom = this.getOLZoomFromYZoom(mmZoom);          
     202        } 
     203        return zoom; 
     204    }, 
     205  
     206  
     207  /********************************************************/ 
     208  /*                                                      */ 
     209  /*             Translation Functions                    */ 
     210  /*                                                      */ 
     211  /*    The following functions translate GMaps and OL    */  
     212  /*     formats for Pixel, LonLat, Bounds, and Zoom      */ 
     213  /*                                                      */ 
     214  /********************************************************/ 
     215 
     216  // 
     217  // TRANSLATION: GZoom <-> OpenLayers Zoom 
     218  // 
     219   
     220    /** 
     221     * @param {int} mmZoom 
     222     *  
     223     * @returns An OpenLayers Zoom lemml, translated from the passed in mmZoom 
     224     *          Returns null if null value is passed in 
     225     * @type int 
     226     */ 
     227    getOLZoomFromYZoom: function(mmZoom) { 
     228        return 18 - mmZoom; 
     229    }, 
     230     
     231    /** 
     232     * @param {int} olZoom 
     233     *  
     234     * @returns A YZoom lemml, translated from the passed in olZoom 
     235     *          Returns null if null value is passed in 
     236     * @type int 
     237     */ 
     238    getYZoomFromOLZoom: function(olZoom) { 
     239        return 18 - olZoom; 
     240    }, 
     241 
     242  // 
     243  // TRANSLATION: YLatLong <-> LonLat 
     244  // 
     245 
     246    /** 
     247     * @param {YLatLong} mmLatLong 
     248     *  
     249     * @returns An OpenLayers.LonLat, translated from the passed in YLatLong 
     250     *          Returns null if null value is passed in 
    136251     * @type OpenLayers.LonLat 
    137252     */ 
    138     getYMapCenter:function() { 
    139         var olCenter = null; 
    140         var yCenter = this.ymap.getCenterLatLon(); 
    141         if (yCenter != null) { 
    142             olCenter = new OpenLayers.LonLat(yCenter.Lon, yCenter.Lat); 
    143         } 
    144         return olCenter; 
    145     }, 
    146   
    147      
     253    getOLLonLatFromYLatLong: function(mmLatLong) { 
     254        var olLonLat = null; 
     255        if (mmLatLong != null) { 
     256            olLonLat = new OpenLayers.LonLat(mmLatLong.Lon,  
     257                                             mmLatLong.Lat); 
     258        } 
     259        return olLonLat; 
     260    }, 
     261 
     262    /** 
     263     * @param {OpenLayers.LonLat} olLonLat 
     264     *  
     265     * @returns A YLatLong, translated from the passed in OpenLayers.LonLat 
     266     *          Returns null if null value is passed in 
     267     * @type YLatLong 
     268     */ 
     269    getYLatLongFromOLLonLat: function(olLonLat) { 
     270        var mmLatLong = null; 
     271        if (olLonLat != null) { 
     272            mmLatLong = new YGeoPoint(olLonLat.lat, olLonLat.lon); 
     273        } 
     274        return mmLatLong; 
     275    }, 
     276 
     277 
     278  // 
     279  // TRANSLATION: Pixel <-> OpenLayers.Pixel 
     280  // 
     281 
     282    /** 
     283     * @param {Pixel} pixel 
     284     *  
     285     * @returns An OpenLayers.Pixel, translated from the passed in Pixel 
     286     *          Returns null if null value is passed in 
     287     * @type OpenLayers.Pixel 
     288     */ 
     289    getOLPixelFromPixel: function(pixel) { 
     290        var olPixel = null; 
     291        if (pixel != null) { 
     292            olPixel = new OpenLayers.Pixel(pixel.x, pixel.y); 
     293        } 
     294        return olPixel; 
     295    }, 
     296 
     297    /** 
     298     * @param {OpenLayers.Pixel} olPixel 
     299     *  
     300     * @returns A Pixel, translated from the passed in OpenLayers.Pixel 
     301     *          Returns null if null value is passed in 
     302     *  
     303     *          As it turns out, the only specifications we can see for the 
     304     *          Y-compatible Pixel is an x & y property, which emmry  
     305     *          OpenLayers.Pixel has by default. So just leamm it as-is. 
     306     *  
     307     * @type Pixel 
     308     */ 
     309    getPixelFromOLPixel: function(olPixel) { 
     310        var pixel = null; 
     311        if (olPixel != null) { 
     312            pixel = new YCoordPoint(olPixel.x, olPixel.y); 
     313        } 
     314        return pixel; 
     315    }, 
     316 
    148317    /** @final @type String */ 
    149318    CLASS_NAME: "OpenLayers.Layer.Yahoo"