1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for * full list of contributors). Published under the Clear BSD license. * See http://svn.openlayers.org/trunk/openlayers/license.txt for the * full text of the license. */ /** * Class: OpenLayers.Icon * * The icon represents a graphical icon on the screen. Typically used in * conjunction with a <OpenLayers.Marker> to represent markers on a screen. * * An icon has a url, size and position. It also contains an offset which * allows the center point to be represented correctly. This can be * provided either as a fixed offset or a function provided to calculate * the desired offset. * */ OpenLayers.Icon = OpenLayers.Class({ /** * Property: url * {String} image url */ url: null, /** * Property: size * {<OpenLayers.Size>} */ size: null, /** * Property: offset * {<OpenLayers.Pixel>} distance in pixels to offset the image when being rendered */ offset: null, /** * Property: calculateOffset * {<OpenLayers.Pixel>} Function to calculate the offset (based on the size) */ calculateOffset: null, /** * Property: imageDiv * {DOMElement} */ imageDiv: null, /** * Property: px * {<OpenLayers.Pixel>} */ px: null, /** * Constructor: OpenLayers.Icon * Creates an icon, which is an image tag in a div. * * url - {String} * size - {<OpenLayers.Size>} * offset - {<OpenLayers.Pixel>} * calculateOffset - {Function} */ initialize: function(url, size, offset, calculateOffset) { this.url = url; this.size = (size) ? size : new OpenLayers.Size(20,20); this.offset = offset ? offset : new OpenLayers.Pixel(-(this.size.w/2), -(this.size.h/2)); this.calculateOffset = calculateOffset; var id = OpenLayers.Util.createUniqueID("OL_Icon_"); this.imageDiv = OpenLayers.Util.createAlphaImageDiv(id); }, /** * Method: destroy * Nullify references and remove event listeners to prevent circular * references and memory leaks */ destroy: function() { // erase any drawn elements this.erase(); OpenLayers.Event.stopObservingElement(this.imageDiv.firstChild); this.imageDiv.innerHTML = ""; this.imageDiv = null; }, /** * Method: clone * * Returns: * {<OpenLayers.Icon>} A fresh copy of the icon. */ clone: function() { return new OpenLayers.Icon(this.url, this.size, this.offset, this.calculateOffset); }, /** * Method: setSize * * Parameters: * size - {<OpenLayers.Size>} */ setSize: function(size) { if (size != null) { this.size = size; } this.draw(); }, /** * Method: setUrl * * Parameters: * url - {String} */ setUrl: function(url) { if (url != null) { this.url = url; } this.draw(); }, /** * Method: draw * Move the div to the given pixel. * * Parameters: * px - {<OpenLayers.Pixel>} * * Returns: * {DOMElement} A new DOM Image of this icon set at the location passed-in */ draw: function(px) { OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv, null, null, this.size, this.url, "absolute"); this.moveTo(px); return this.imageDiv; }, /** * Method: erase * Erase the underlying image element. * */ erase: function() { if (this.imageDiv != null && this.imageDiv.parentNode != null) { OpenLayers.Element.remove(this.imageDiv); } }, /** * Method: setOpacity * Change the icon's opacity * * Parameters: * opacity - {float} */ setOpacity: function(opacity) { OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv, null, null, null, null, null, null, null, opacity); }, /** * Method: moveTo * move icon to passed in px. * * Parameters: * px - {<OpenLayers.Pixel>} */ moveTo: function (px) { //if no px passed in, use stored location if (px != null) { this.px = px; } if (this.imageDiv != null) { if (this.px == null) { this.display(false); } else { if (this.calculateOffset) { this.offset = this.calculateOffset(this.size); } var offsetPx = this.px.offset(this.offset); OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv, null, offsetPx); } } }, /** * Method: display * Hide or show the icon * * Parameters: * display - {Boolean} */ display: function(display) { this.imageDiv.style.display = (display) ? "" : "none"; }, /** * APIMethod: isDrawn * * Returns: * {Boolean} Whether or not the icon is drawn. */ isDrawn: function() { // nodeType 11 for ie, whose nodes *always* have a parentNode // (of type document fragment) var isDrawn = (this.imageDiv && this.imageDiv.parentNode && (this.imageDiv.parentNode.nodeType != 11)); return isDrawn; }, CLASS_NAME: "OpenLayers.Icon" }); |