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.Marker = Class.create();
9
OpenLayers.Marker.prototype = {
10
    
11
    /** @type OpenLayers.Icon */
12
    icon: null,
13

    
14
    /** location of object
15
    * @type OpenLayers.LonLat */
16
    lonlat: null,
17
    
18
    /** @type OpenLayers.Events*/
19
    events: null,
20
    
21
    /** @type OpenLayers.Map */
22
    map: null,
23
    
24
    /** 
25
    * @constructor
26
    *
27
    * @param {OpenLayers.Icon} icon
28
    * @param {OpenLayers.LonLat lonlat
29
    */
30
    initialize: function(lonlat, icon) {
31
        if (arguments.length > 0) {
32
            this.lonlat = lonlat;
33
            this.icon = (icon) ? icon : OpenLayers.Marker.defaultIcon();
34
            this.events = new OpenLayers.Events(this, this.icon.imageDiv, null);
35
        }
36
    },
37
    
38
    destroy: function() {
39
        this.map = null;
40
        
41
        if (this.icon != null) {
42
            this.icon.destroy();
43
            this.icon = null;
44
        }
45
    },
46
    
47
    /** 
48
    * @param {OpenLayers.Pixel} px
49
    * 
50
    * @return A new DOM Image with this marker?s icon set at the 
51
    *         location passed-in
52
    * @type DOMElement
53
    */
54
    draw: function(px) {
55
        return this.icon.draw(px);
56
    }, 
57

    
58
    /**
59
    * @param {OpenLayers.Pixel} px
60
    */
61
    moveTo: function (px) {
62
        if ((px != null) && (this.icon != null)) {
63
            this.icon.moveTo(px);
64
        }            
65
    },
66

    
67
    /**
68
     * @returns Whether or not the marker is currently visible on screen.
69
     * @type Boolean
70
     */
71
    onScreen:function() {
72
        
73
        var onScreen = false;
74
        if (this.map) {
75
            var screenBounds = this.map.getExtent();
76
            onScreen = screenBounds.contains(this.lonlat.lon, this.lonlat.lat);
77
        }    
78
        return onScreen;
79
    },
80
    
81
    /**
82
     * @param {float} inflate
83
     */
84
    inflate: function(inflate) {
85
        if (this.icon) {
86
            var newSize = new OpenLayers.Size(this.icon.size.w * inflate,
87
                                              this.icon.size.h * inflate);
88
            this.icon.setSize(newSize);
89
        }        
90
    },
91
    
92
    /** Hide or show the icon
93
     * 
94
     * @param {Boolean} display
95
     */
96
    display: function(display) {
97
        this.icon.display(display);
98
    },
99
    
100
    /** @final @type String */
101
    CLASS_NAME: "OpenLayers.Marker"
102
};
103

    
104

    
105
/** 
106
 * @returns A default OpenLayers.Icon to use for a marker
107
 * @type OpenLayers.Icon
108
 */
109
OpenLayers.Marker.defaultIcon = function() {
110
    var url = OpenLayers.Util.getImagesLocation() + "marker.png";
111
    var size = new OpenLayers.Size(21, 25);
112
    var calculateOffset = function(size) {
113
                    return new OpenLayers.Pixel(-(size.w/2), -size.h);
114
                 };
115

    
116
    return new OpenLayers.Icon(url, size, null, calculateOffset);        
117
};
118
    
119

    
(9-9/13)