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 * Class: OpenLayers.Icon
9 * The icon represents a graphical icon on the screen. Typically used in
10 * conjunction with a <OpenLayers.Marker> to represent markers on a screen.
12 * An icon has a url, size and position. It also contains an offset which
13 * allows the center point to be represented correctly. This can be
14 * provided either as a fixed offset or a function provided to calculate
18 OpenLayers.Icon = OpenLayers.Class({
34 * {<OpenLayers.Pixel>} distance in pixels to offset the image when being rendered
39 * Property: calculateOffset
40 * {<OpenLayers.Pixel>} Function to calculate the offset (based on the size)
42 calculateOffset: null,
52 * {<OpenLayers.Pixel>}
57 * Constructor: OpenLayers.Icon
58 * Creates an icon, which is an image tag in a div.
61 * size - {<OpenLayers.Size>}
62 * offset - {<OpenLayers.Pixel>}
63 * calculateOffset - {Function}
65 initialize: function(url, size, offset, calculateOffset) {
67 this.size = (size) ? size : new OpenLayers.Size(20,20);
68 this.offset = offset ? offset : new OpenLayers.Pixel(-(this.size.w/2), -(this.size.h/2));
69 this.calculateOffset = calculateOffset;
71 var id = OpenLayers.Util.createUniqueID("OL_Icon_");
72 this.imageDiv = OpenLayers.Util.createAlphaImageDiv(id);
77 * Nullify references and remove event listeners to prevent circular
78 * references and memory leaks
81 // erase any drawn elements
84 OpenLayers.Event.stopObservingElement(this.imageDiv.firstChild);
85 this.imageDiv.innerHTML = "";
93 * {<OpenLayers.Icon>} A fresh copy of the icon.
96 return new OpenLayers.Icon(this.url,
99 this.calculateOffset);
106 * size - {<OpenLayers.Size>}
108 setSize: function(size) {
121 setUrl: function(url) {
130 * Move the div to the given pixel.
133 * px - {<OpenLayers.Pixel>}
136 * {DOMElement} A new DOM Image of this icon set at the location passed-in
139 OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv,
146 return this.imageDiv;
151 * Erase the underlying image element.
155 if (this.imageDiv != null && this.imageDiv.parentNode != null) {
156 OpenLayers.Element.remove(this.imageDiv);
162 * Change the icon's opacity
167 setOpacity: function(opacity) {
168 OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv, null, null, null,
169 null, null, null, null, opacity);
175 * move icon to passed in px.
178 * px - {<OpenLayers.Pixel>}
180 moveTo: function (px) {
181 //if no px passed in, use stored location
186 if (this.imageDiv != null) {
187 if (this.px == null) {
190 if (this.calculateOffset) {
191 this.offset = this.calculateOffset(this.size);
193 var offsetPx = this.px.offset(this.offset);
194 OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv, null, offsetPx);
201 * Hide or show the icon
204 * display - {Boolean}
206 display: function(display) {
207 this.imageDiv.style.display = (display) ? "" : "none";
215 * {Boolean} Whether or not the icon is drawn.
217 isDrawn: function() {
218 // nodeType 11 for ie, whose nodes *always* have a parentNode
219 // (of type document fragment)
220 var isDrawn = (this.imageDiv && this.imageDiv.parentNode &&
221 (this.imageDiv.parentNode.nodeType != 11));
226 CLASS_NAME: "OpenLayers.Icon"