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. */
7 * @requires OpenLayers/Layer.js
11 * Class: OpenLayers.Layer.Markers
14 * - <OpenLayers.Layer>
16 OpenLayers.Layer.Markers = OpenLayers.Class(OpenLayers.Layer, {
19 * APIProperty: isBaseLayer
20 * {Boolean} Markers layer is never a base layer.
25 * APIProperty: markers
26 * {Array(<OpenLayers.Marker>)} internal marker list
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.
41 * Constructor: OpenLayers.Layer.Markers
42 * Create a Markers layer.
46 * options - {Object} Hashtable of extra options to tag onto the layer
48 initialize: function(name, options) {
49 OpenLayers.Layer.prototype.initialize.apply(this, arguments);
59 OpenLayers.Layer.prototype.destroy.apply(this, arguments);
63 * APIMethod: setOpacity
64 * Sets the opacity for all the markers.
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);
82 * bounds - {<OpenLayers.Bounds>}
83 * zoomChanged - {Boolean}
84 * dragging - {Boolean}
86 moveTo:function(bounds, zoomChanged, dragging) {
87 OpenLayers.Layer.prototype.moveTo.apply(this, arguments);
89 if (zoomChanged || !this.drawn) {
90 for(var i=0, len=this.markers.length; i<len; i++) {
91 this.drawMarker(this.markers[i]);
98 * APIMethod: addMarker
101 * marker - {<OpenLayers.Marker>}
103 addMarker: function(marker) {
104 this.markers.push(marker);
106 if (this.opacity != null) {
107 marker.setOpacity(this.opacity);
110 if (this.map && this.map.getExtent()) {
111 marker.map = this.map;
112 this.drawMarker(marker);
117 * APIMethod: removeMarker
120 * marker - {<OpenLayers.Marker>}
122 removeMarker: function(marker) {
123 if (this.markers && this.markers.length) {
124 OpenLayers.Util.removeItem(this.markers, marker);
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.
134 clearMarkers: function() {
135 if (this.markers != null) {
136 while(this.markers.length > 0) {
137 this.removeMarker(this.markers[0]);
144 * Calculate the pixel location for the marker, create it, and
145 * add it to the layer's div
148 * marker - {<OpenLayers.Marker>}
150 drawMarker: function(marker) {
151 var px = this.map.getLayerPxFromLonLat(marker.lonlat);
153 marker.display(false);
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);
165 * APIMethod: getDataExtent
166 * Calculates the max extent which includes all of the markers.
169 * {<OpenLayers.Bounds>}
171 getDataExtent: function () {
172 var maxExtent = null;
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);
185 CLASS_NAME: "OpenLayers.Layer.Markers"