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