]> git.openstreetmap.org Git - rails.git/blob - public/lib/OpenLayers/Marker.js
If a search only finds one result then jump straight to it instead of
[rails.git] / public / lib / OpenLayers / Marker.js
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 * @class
6 */
7 OpenLayers.Marker = Class.create();
8 OpenLayers.Marker.prototype = {
9     
10     /** @type OpenLayers.Icon */
11     icon: null,
12
13     /** location of object
14     * @type OpenLayers.LonLat */
15     lonlat: null,
16     
17     /** @type OpenLayers.Events*/
18     events: null,
19     
20     /** @type OpenLayers.Map */
21     map: null,
22     
23     /** 
24     * @constructor
25     *
26     * @param {OpenLayers.Icon} icon
27     * @param {OpenLayers.LonLat lonlat
28     */
29     initialize: function(lonlat, icon) {
30         this.lonlat = lonlat;
31         this.icon = (icon) ? icon : OpenLayers.Marker.defaultIcon();
32
33         this.events = new OpenLayers.Events(this, this.icon.imageDiv, null);
34     },
35     
36     destroy: function() {
37         this.map = null;
38         
39         if (this.icon != null) {
40             this.icon.destroy();
41             this.icon = null;
42         }
43     },
44     
45     /** 
46     * @param {OpenLayers.Pixel} px
47     * 
48     * @return A new DOM Image with this markerĀ“s icon set at the 
49     *         location passed-in
50     * @type DOMElement
51     */
52     draw: function(px) {
53         return this.icon.draw(px);
54     }, 
55
56     /**
57     * @param {OpenLayers.Pixel} px
58     */
59     moveTo: function (px) {
60         if ((px != null) && (this.icon != null)) {
61             this.icon.moveTo(px);
62         }            
63     },
64
65     /**
66      * @returns Whether or not the marker is currently visible on screen.
67      * @type Boolean
68      */
69     onScreen:function() {
70         
71         var onScreen = false;
72         if (this.map) {
73             var screenBounds = this.map.getExtent();
74             onScreen = screenBounds.contains(this.lonlat.lon, this.lonlat.lat);
75         }    
76         return onScreen;
77     },
78     
79     /**
80      * @param {float} inflate
81      */
82     inflate: function(inflate) {
83         if (this.icon) {
84             var newSize = new OpenLayers.Size(this.icon.size.w * inflate,
85                                               this.icon.size.h * inflate);
86             this.icon.setSize(newSize);
87         }        
88     },
89     
90     /** @final @type String */
91     CLASS_NAME: "OpenLayers.Marker"
92 };
93
94
95 /** 
96  * @returns A default OpenLayers.Icon to use for a marker
97  * @type OpenLayers.Icon
98  */
99 OpenLayers.Marker.defaultIcon = function() {
100     var url = OpenLayers.Util.getImagesLocation() + "marker.png";
101     var size = new OpenLayers.Size(21, 25);
102     var calculateOffset = function(size) {
103                     return new OpenLayers.Pixel(-(size.w/2), -size.h);
104                  };
105
106     return new OpenLayers.Icon(url, size, null, calculateOffset);        
107 };
108     
109