OpenLayers OpenLayers

Changeset 910

Show
Ignore:
Timestamp:
07/07/06 08:55:39 (2 years ago)
Author:
euzuro
Message:

remove document.write() call that loaded the gmaps script in google.js. from now on, the user is responsible for adding that script tag to his/her page. Add a test to make sure gmaps code base is loaded before trying to initialize new gmap2. update isBaseLayer() to return true only if gmaps is correctly loaded. finally, protect all methods if this.gmap is null, do nothing. update OpenLayers.js to now include Google.js and library.cfg to not exclude it.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/build/library.cfg

    r773 r910  
    88 
    99[exclude] 
    10 OpenLayers/Layer/Google.js 
    1110OpenLayers/Layer/Yahoo.js 
    1211OpenLayers/Layer/VirtualEarth.js 
  • trunk/openlayers/lib/OpenLayers.js

    r886 r910  
    6565        "OpenLayers/Tile/Image.js", 
    6666        "OpenLayers/Tile/WFS.js", 
    67 //        "OpenLayers/Layer/Google.js", 
     67        "OpenLayers/Layer/Google.js", 
    6868//        "OpenLayers/Layer/VirtualEarth.js", 
    6969//        "OpenLayers/Layer/Yahoo.js", 
  • trunk/openlayers/lib/OpenLayers/Layer/Google.js

    r836 r910  
    33 * text of the license. */ 
    44// @require: OpenLayers/Layer.js 
    5  
    6 // load Google map control script 
    7 // this key was generated for: http://openlayers.python-hosting.com/testing/euzuro/ 
    8 document.write("<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAmQ3udCHPQVB_9T_edFZ7YRRRlP-tOiFgaSzksg_0w1dphL9c5BTfdJMKT91b0UJGibNcWEM0Q5-O1w'></script>"); 
    95 
    106/** 
     
    5955     */ 
    6056    isBaseLayer: function() { 
    61         return true
     57        return (this.gmap != null)
    6258    }, 
    6359     
     
    9692    loadGMap:function() { 
    9793         
    98         // create GMap, hide nav controls 
    99         this.gmap = new GMap2(this.div); 
    100  
    101         // this causes the GMap to set itself to Map's center/zoom 
    102         this.moveTo(); 
    103  
    104         // catch pans and zooms from GMap 
    105         GEvent.addListener(this.gmap,  
    106                            "moveend",  
    107                            this.catchPanZoom.bindAsEventListener(this));  
    108  
    109  
    110         // attach to the drag start and end and weŽll set a flag so that 
    111         //  we dont get recursivity. this is because when we call setCenter(), 
    112         //  it calls moveTo() on all layers 
    113         GEvent.addListener(this.gmap,  
    114                            "dragstart",  
    115                            this.dragStart.bindAsEventListener(this));  
    116  
    117         GEvent.addListener(this.gmap,  
    118                            "dragend",  
    119                            this.dragEnd.bindAsEventListener(this));  
    120  
    121         // catch pans and zooms from GMap 
    122         GEvent.addListener(this.gmap,  
    123                            "drag",  
    124                            this.catchPanZoom.bindAsEventListener(this));  
    125  
     94        //test if the gmaps library has been loaded 
     95        var gmapsLoaded = (typeof GMap2) != "undefined"; 
     96        if (gmapsLoaded) { 
     97          
     98            // create GMap, hide nav controls 
     99            this.gmap = new GMap2(this.div); 
     100     
     101            // this causes the GMap to set itself to Map's center/zoom 
     102            this.moveTo(); 
     103     
     104            // catch pans and zooms from GMap 
     105            GEvent.addListener(this.gmap,  
     106                               "moveend",  
     107                               this.catchPanZoom.bindAsEventListener(this));  
     108     
     109     
     110            // attach to the drag start and end and weŽll set a flag so that 
     111            //  we dont get recursivity. this is because when we call setCenter(), 
     112            //  it calls moveTo() on all layers 
     113            GEvent.addListener(this.gmap,  
     114                               "dragstart",  
     115                               this.dragStart.bindAsEventListener(this));  
     116     
     117            GEvent.addListener(this.gmap,  
     118                               "dragend",  
     119                               this.dragEnd.bindAsEventListener(this));  
     120     
     121            // catch pans and zooms from GMap 
     122            GEvent.addListener(this.gmap,  
     123                               "drag",  
     124                               this.catchPanZoom.bindAsEventListener(this));  
     125        } 
    126126    }, 
    127127 
     
    162162     * @returns An OpenLayers.LonLat which is the passed-in view port 
    163163     *          OpenLayers.Pixel, translated into lon/lat by GMAPS 
     164     *          If gmap is not loaded, returns null. 
    164165     * @type OpenLayers.LonLat 
    165166     */ 
    166167    getLonLatFromViewPortPx: function (viewPortPx) { 
    167         var gPoint = this.getGPointFromOLPixel(viewPortPx); 
    168         var gLatLng = this.gmap.fromContainerPixelToLatLng(gPoint) 
    169          
    170         return this.getOLLonLatFromGLatLng(gLatLng); 
     168        var lonlat = null; 
     169        if (this.gmap != null) { 
     170            var gPoint = this.getGPointFromOLPixel(viewPortPx); 
     171            var gLatLng = this.gmap.fromContainerPixelToLatLng(gPoint) 
     172            lonlat = this.getOLLonLatFromGLatLng(gLatLng); 
     173        } 
     174        return lonlat; 
    171175    }, 
    172176 
     
    177181     * @returns An OpenLayers.Pixel which is the passed-in OpenLayers.LonLat,  
    178182     *          translated into view port pixels BY GMAPS 
     183     *          If gmap is not loaded, returns null. 
    179184     * @type OpenLayers.Pixel 
    180185     */ 
    181     getViewPortPxFromLonLat: function (lonlat) {     
    182         var gLatLng = this.getGLatLngFromOLLonLat(lonlat); 
    183  
    184         // note we use special hacked function here,  
    185         // because GMaps doesnt give it to us 
    186         var gPoint = this.fromLatLngToContainerPixel(gLatLng); 
    187  
    188         return this.getOLPixelFromGPoint(gPoint); 
     186    getViewPortPxFromLonLat: function (lonlat) { 
     187        var viewPortPx = null; 
     188        if (this.gmap != null) { 
     189            var gLatLng = this.getGLatLngFromOLLonLat(lonlat); 
     190         
     191            // note we use special hacked function here,  
     192            // because GMaps doesnt give it to us 
     193            var gPoint = this.fromLatLngToContainerPixel(gLatLng); 
     194         
     195            viewPortPx = this.getOLPixelFromGPoint(gPoint); 
     196        } 
     197        return viewPortPx; 
    189198    }, 
    190199 
     
    218227     * @param {OpenLayers.Bounds} bounds 
    219228     * 
    220      * @return {int} 
     229     * @returns Corresponding zoom level for a specified Bounds.  
     230     *          If gmap is not loaded, returns null. 
     231     * @type int 
    221232     */ 
    222233    getZoomForExtent: function (bounds) { 
    223         var gBounds = this.getGLatLngBoundsFromOLBounds(bounds); 
    224         var gZoom = this.gmap.getBoundsZoomLevel(gBounds); 
    225  
    226         return this.getOLZoomFromGZoom(gZoom); 
     234        var zoom = null; 
     235        if (this.gmap != null) { 
     236            var gBounds = this.getGLatLngBoundsFromOLBounds(bounds); 
     237            var gZoom = this.gmap.getBoundsZoomLevel(gBounds); 
     238            zoom = this.getOLZoomFromGZoom(gZoom); 
     239        } 
     240        return zoom; 
    227241    }, 
    228242 
     
    230244     * @returns A Bounds object which represents the lon/lat bounds of the  
    231245     *          current viewPort. 
     246     *          If gmap is not loaded, returns null 
    232247     * @type OpenLayers.Bounds 
    233248     */ 
    234249    getExtent: function () { 
    235         if (this.gmap.getCenter() == null) { 
    236             this.moveTo(); 
    237         } 
    238         var gLatLngBounds = this.gmap.getBounds(); 
    239         return this.getOLBoundsFromGLatLngBounds(gLatLngBounds); 
     250        var extent = null; 
     251        if (this.gmap != null) { 
     252            if (this.gmap.getCenter() == null) { 
     253                this.moveTo(); 
     254            } 
     255            var gLatLngBounds = this.gmap.getBounds(); 
     256            extent = this.getOLBoundsFromGLatLngBounds(gLatLngBounds); 
     257        } 
     258        return extent; 
    240259    }, 
    241260