]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Layer/Markers.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Layer / Markers.js
1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2  * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3  * full text of the license. */
4
5
6 /**
7  * @requires OpenLayers/Layer.js
8  */
9
10 /**
11  * Class: OpenLayers.Layer.Markers
12  * 
13  * Inherits from:
14  *  - <OpenLayers.Layer> 
15  */
16 OpenLayers.Layer.Markers = OpenLayers.Class(OpenLayers.Layer, {
17     
18     /** 
19      * APIProperty: isBaseLayer 
20      * {Boolean} Markers layer is never a base layer.  
21      */
22     isBaseLayer: false,
23     
24     /** 
25      * APIProperty: markers 
26      * {Array(<OpenLayers.Marker>)} internal marker list 
27      */
28     markers: null,
29
30
31     /** 
32      * Property: drawn 
33      * {Boolean} internal state of drawing. This is a workaround for the fact
34      * that the map does not call moveTo with a zoomChanged when the map is
35      * first starting up. This lets us catch the case where we have *never*
36      * drawn the layer, and draw it even if the zoom hasn't changed.
37      */
38     drawn: false,
39     
40     /**
41      * Constructor: OpenLayers.Layer.Markers 
42      * Create a Markers layer.
43      *
44      * Parameters:
45      * name - {String} 
46      * options - {Object} Hashtable of extra options to tag onto the layer
47      */
48     initialize: function(name, options) {
49         OpenLayers.Layer.prototype.initialize.apply(this, arguments);
50         this.markers = [];
51     },
52     
53     /**
54      * APIMethod: destroy 
55      */
56     destroy: function() {
57         this.clearMarkers();
58         this.markers = null;
59         OpenLayers.Layer.prototype.destroy.apply(this, arguments);
60     },
61
62     /**
63      * APIMethod: setOpacity
64      * Sets the opacity for all the markers.
65      * 
66      * Parameter:
67      * opacity - {Float}
68      */
69     setOpacity: function(opacity) {
70         if (opacity != this.opacity) {
71             this.opacity = opacity;
72             for (var i=0, len=this.markers.length; i<len; i++) {
73                 this.markers[i].setOpacity(this.opacity);
74             }
75         }
76     },
77
78     /** 
79      * Method: moveTo
80      *
81      * Parameters:
82      * bounds - {<OpenLayers.Bounds>} 
83      * zoomChanged - {Boolean} 
84      * dragging - {Boolean} 
85      */
86     moveTo:function(bounds, zoomChanged, dragging) {
87         OpenLayers.Layer.prototype.moveTo.apply(this, arguments);
88
89         if (zoomChanged || !this.drawn) {
90             for(var i=0, len=this.markers.length; i<len; i++) {
91                 this.drawMarker(this.markers[i]);
92             }
93             this.drawn = true;
94         }
95     },
96
97     /**
98      * APIMethod: addMarker
99      *
100      * Parameters:
101      * marker - {<OpenLayers.Marker>} 
102      */
103     addMarker: function(marker) {
104         this.markers.push(marker);
105
106         if (this.opacity != null) {
107             marker.setOpacity(this.opacity);
108         }
109
110         if (this.map && this.map.getExtent()) {
111             marker.map = this.map;
112             this.drawMarker(marker);
113         }
114     },
115
116     /**
117      * APIMethod: removeMarker
118      *
119      * Parameters:
120      * marker - {<OpenLayers.Marker>} 
121      */
122     removeMarker: function(marker) {
123         if (this.markers && this.markers.length) {
124             OpenLayers.Util.removeItem(this.markers, marker);
125             marker.erase();
126         }
127     },
128
129     /**
130      * Method: clearMarkers
131      * This method removes all markers from a layer. The markers are not
132      * destroyed by this function, but are removed from the list of markers.
133      */
134     clearMarkers: function() {
135         if (this.markers != null) {
136             while(this.markers.length > 0) {
137                 this.removeMarker(this.markers[0]);
138             }
139         }
140     },
141
142     /** 
143      * Method: drawMarker
144      * Calculate the pixel location for the marker, create it, and 
145      *    add it to the layer's div
146      *
147      * Parameters:
148      * marker - {<OpenLayers.Marker>} 
149      */
150     drawMarker: function(marker) {
151         var px = this.map.getLayerPxFromLonLat(marker.lonlat);
152         if (px == null) {
153             marker.display(false);
154         } else {
155             if (!marker.isDrawn()) {
156                 var markerImg = marker.draw(px);
157                 this.div.appendChild(markerImg);
158             } else if(marker.icon) {
159                 marker.icon.moveTo(px);
160             }
161         }
162     },
163     
164     /** 
165      * APIMethod: getDataExtent
166      * Calculates the max extent which includes all of the markers.
167      * 
168      * Returns:
169      * {<OpenLayers.Bounds>}
170      */
171     getDataExtent: function () {
172         var maxExtent = null;
173         
174         if ( this.markers && (this.markers.length > 0)) {
175             var maxExtent = new OpenLayers.Bounds();
176             for(var i=0, len=this.markers.length; i<len; i++) {
177                 var marker = this.markers[i];
178                 maxExtent.extend(marker.lonlat);
179             }
180         }
181
182         return maxExtent;
183     },
184
185     CLASS_NAME: "OpenLayers.Layer.Markers"
186 });