Project

General

Profile

1
/* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
2
 * See http://svn.openlayers.org/trunk/openlayers/license.txt for the full
3
 * text of the license. */
4

    
5
/**
6
 * @class
7
 */
8
OpenLayers.Icon = Class.create();
9
OpenLayers.Icon.prototype = {
10
    
11
    /** image url
12
    * @type String */
13
    url: null,
14
    
15
    /** @type OpenLayers.Size */
16
    size:null,
17

    
18
    /** distance in pixels to offset the image when being rendered
19
    * @type OpenLayers.Pixel */
20
    offset: null,    
21
    
22
    /** Function to calculate the offset (based on the size) 
23
     * @type OpenLayers.Pixel */
24
    calculateOffset: null,    
25
    
26
    /** @type DOMElement */
27
    imageDiv: null,
28

    
29
    /** @type OpenLayers.Pixel */
30
    px: null,
31
    
32
    /** 
33
    * @constructor
34
    *
35
    * @param {String} url
36
    * @param {OpenLayers.Size} size
37
    * @param {Function} calculateOffset
38
    */
39
    initialize: function(url, size, offset, calculateOffset) {
40
        this.url = url;
41
        this.size = (size) ? size : new OpenLayers.Size(20,20);
42
        this.offset = (offset) ? offset : new OpenLayers.Pixel(0,0);
43
        this.calculateOffset = calculateOffset;
44

    
45
        this.imageDiv = OpenLayers.Util.createAlphaImageDiv();
46
    },
47
    
48
    destroy: function() {
49
        this.imageDiv = null;
50
    },
51

    
52
    /** 
53
    * @returns A fresh copy of the icon.
54
    * @type OpenLayers.Icon
55
    */
56
    clone: function() {
57
        return new OpenLayers.Icon(this.url, 
58
                                   this.size, 
59
                                   this.offset, 
60
                                   this.calculateOffset);
61
    },
62
    
63
    /**
64
     * @param {OpenLayers.Size} size
65
     */
66
    setSize: function(size) {
67
        if (size != null) {
68
            this.size = size;
69
        }
70
        this.draw();
71
    },
72

    
73
    /** 
74
    * @param {OpenLayers.Pixel} px
75
    * 
76
    * @return A new DOM Image of this icon set at the location passed-in
77
    * @type DOMElement
78
    */
79
    draw: function(px) {
80
        OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv, 
81
                                            null, 
82
                                            null, 
83
                                            this.size, 
84
                                            this.url, 
85
                                            "absolute");
86
        this.moveTo(px);
87
        return this.imageDiv;
88
    }, 
89

    
90
    /**
91
    * @param {OpenLayers.Pixel} px
92
    */
93
    moveTo: function (px) {
94
        //if no px passed in, use stored location
95
        if (px != null) {
96
            this.px = px;
97
        }
98

    
99
        if (this.imageDiv != null) {
100
            if (this.px == null) {
101
                this.display(false);
102
            } else {
103
                if (this.calculateOffset) {
104
                    this.offset = this.calculateOffset(this.size);  
105
                }
106
                var offsetPx = this.px.offset(this.offset);
107
                OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv, null, offsetPx);
108
                this.display(true);
109
            }
110
        }
111
    },
112
    
113
    /** Hide or show the icon
114
     * 
115
     * @param {Boolean} display
116
     */
117
    display: function(display) {
118
        this.imageDiv.style.display = (display) ? "" : "none"; 
119
    },
120
    
121
    /** @final @type String */
122
    CLASS_NAME: "OpenLayers.Icon"
123
};
(6-6/13)