]> git.openstreetmap.org Git - rails.git/blob - public/lib/OpenLayers/Layer/Google.js
Cope with not having a layer configuration at all.
[rails.git] / public / lib / OpenLayers / Layer / Google.js
1 /* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
2  * See http://svn.openlayers.org/trunk/openlayers/license.txt for the full
3  * text of the license. */
4 // @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>");
9
10 /**
11  * @class
12  */
13 OpenLayers.Layer.Google = Class.create();
14 OpenLayers.Layer.Google.prototype = Object.extend( new OpenLayers.Layer(), {
15
16     /** @type Boolean */
17     viewPortLayer: true,
18
19     /** @type GMap2 gmap stores the Google Map element */
20     gmap:null,
21    
22     /** @type Boolean */
23     dragging:false,
24     
25     /** 
26      * @constructor
27      * 
28      * @param {String} name
29      */
30     initialize: function(name) {
31         OpenLayers.Layer.prototype.initialize.apply(this, [name]);
32     },
33     
34      /** 
35      * @param {OpenLayers.Map} map
36      */
37     setMap:function(map) {
38         OpenLayers.Layer.prototype.setMap.apply(this, arguments);
39
40         // once our layer has been added to the map, we can create the vemap
41         this.map.events.register("addlayer", this, this.loadGMap);
42     },
43     
44     /** Google layer is always a base class.
45      * @type Boolean
46      */
47     isBaseLayer: function() {
48         return true;
49     },
50     
51     /** 
52      * @param {OpenLayers.Bounds} bounds
53      * @param {int} zoomChanged
54      */
55     moveTo:function(bounds,zoomChanged) {
56
57         if ((this.gmap != null) && (!this.dragging)) {
58
59             var olCenter = this.map.getCenter();
60             var gCenter = this.getGMapCenter();
61             
62             var olZoom = this.map.getZoom();
63             var gZoom = this.gmap.getZoom();
64             
65             if ((!olCenter.equals(gCenter)) || ((olZoom +1) != gZoom)) {
66                 this.gmap.setCenter(new GLatLng(olCenter.lat, olCenter.lon), 
67                                     olZoom + 1);
68             }
69         }
70     },
71
72     /**
73      * 
74      */
75     loadGMap:function() {
76         // create div and set to same size as map
77         var gDiv = OpenLayers.Util.createDiv(this.name);
78         var sz = this.map.getSize();
79         gDiv.style.width = sz.w;
80         gDiv.style.height = sz.h;
81         this.div.appendChild(gDiv);
82
83         // create GMap, hide nav controls
84         this.gmap = new GMap2(this.div);
85         this.moveTo();
86
87         // catch pans and zooms from GMap
88         GEvent.addListener(this.gmap, 
89                            "moveend", 
90                            this.catchPanZoom.bindAsEventListener(this)); 
91
92
93         // attach to the drag start and end and we´ll set a flag so that
94         //  we dont get recursivity. this is because the events fall through
95         //  the gmaps div and into the main layer div
96         GEvent.addListener(this.gmap, 
97                            "dragstart", 
98                            this.dragStart.bindAsEventListener(this)); 
99
100         GEvent.addListener(this.gmap, 
101                            "dragend", 
102                            this.dragEnd.bindAsEventListener(this)); 
103
104     },
105
106     /** 
107      * @private
108      */
109     dragStart: function() {
110         this.dragging = true;
111     },
112     
113     /** 
114      * @private
115      */
116     dragEnd: function() {
117         this.dragging = false;
118     },
119     
120     /**
121      * @private 
122      * 
123      * @param {event} e
124      */
125     catchPanZoom: function(e) { 
126         var olCenter = this.getGMapCenter();
127         var gZoom = this.gmap.getZoom();
128
129         this.map.setCenter(olCenter, gZoom - 1);
130         
131     },
132
133     /**
134      * @private
135      * 
136      * @returns An OpenLayers.LonLat with the center of the gmap, or null if 
137      *           the GMap has not been centered yet
138      * @type OpenLayers.LonLat
139      */
140     getGMapCenter:function() {
141         var olCenter = null;
142         var gCenter = this.gmap.getCenter();
143         if (gCenter != null) {
144             olCenter = new OpenLayers.LonLat(gCenter.lng(), gCenter.lat());
145         }
146         return olCenter;
147     },
148  
149     
150     /** @final @type String */
151     CLASS_NAME: "OpenLayers.Layer.Google"
152 });