1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | |
---|
7 | /** |
---|
8 | * @requires OpenLayers/Events.js |
---|
9 | * @requires OpenLayers/Icon.js |
---|
10 | */ |
---|
11 | |
---|
12 | /** |
---|
13 | * Class: OpenLayers.Marker |
---|
14 | * Instances of OpenLayers.Marker are a combination of a |
---|
15 | * <OpenLayers.LonLat> and an <OpenLayers.Icon>. |
---|
16 | * |
---|
17 | * Markers are generally added to a special layer called |
---|
18 | * <OpenLayers.Layer.Markers>. |
---|
19 | * |
---|
20 | * Example: |
---|
21 | * (code) |
---|
22 | * var markers = new OpenLayers.Layer.Markers( "Markers" ); |
---|
23 | * map.addLayer(markers); |
---|
24 | * |
---|
25 | * var size = new OpenLayers.Size(21,25); |
---|
26 | * var offset = new OpenLayers.Pixel(-(size.w/2), -size.h); |
---|
27 | * var icon = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker.png', size, offset); |
---|
28 | * markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0),icon)); |
---|
29 | * markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0),icon.clone())); |
---|
30 | * |
---|
31 | * (end) |
---|
32 | * |
---|
33 | * Note that if you pass an icon into the Marker constructor, it will take |
---|
34 | * that icon and use it. This means that you should not share icons between |
---|
35 | * markers -- you use them once, but you should clone() for any additional |
---|
36 | * markers using that same icon. |
---|
37 | */ |
---|
38 | OpenLayers.Marker = OpenLayers.Class({ |
---|
39 | |
---|
40 | /** |
---|
41 | * Property: icon |
---|
42 | * {<OpenLayers.Icon>} The icon used by this marker. |
---|
43 | */ |
---|
44 | icon: null, |
---|
45 | |
---|
46 | /** |
---|
47 | * Property: lonlat |
---|
48 | * {<OpenLayers.LonLat>} location of object |
---|
49 | */ |
---|
50 | lonlat: null, |
---|
51 | |
---|
52 | /** |
---|
53 | * Property: events |
---|
54 | * {<OpenLayers.Events>} the event handler. |
---|
55 | */ |
---|
56 | events: null, |
---|
57 | |
---|
58 | /** |
---|
59 | * Property: map |
---|
60 | * {<OpenLayers.Map>} the map this marker is attached to |
---|
61 | */ |
---|
62 | map: null, |
---|
63 | |
---|
64 | /** |
---|
65 | * Constructor: OpenLayers.Marker |
---|
66 | * Parameters: |
---|
67 | * lonlat - {<OpenLayers.LonLat>} the position of this marker |
---|
68 | * icon - {<OpenLayers.Icon>} the icon for this marker |
---|
69 | */ |
---|
70 | initialize: function(lonlat, icon) { |
---|
71 | this.lonlat = lonlat; |
---|
72 | |
---|
73 | var newIcon = (icon) ? icon : OpenLayers.Marker.defaultIcon(); |
---|
74 | if (this.icon == null) { |
---|
75 | this.icon = newIcon; |
---|
76 | } else { |
---|
77 | this.icon.url = newIcon.url; |
---|
78 | this.icon.size = newIcon.size; |
---|
79 | this.icon.offset = newIcon.offset; |
---|
80 | this.icon.calculateOffset = newIcon.calculateOffset; |
---|
81 | } |
---|
82 | this.events = new OpenLayers.Events(this, this.icon.imageDiv, null); |
---|
83 | }, |
---|
84 | |
---|
85 | /** |
---|
86 | * APIMethod: destroy |
---|
87 | * Destroy the marker. You must first remove the marker from any |
---|
88 | * layer which it has been added to, or you will get buggy behavior. |
---|
89 | * (This can not be done within the marker since the marker does not |
---|
90 | * know which layer it is attached to.) |
---|
91 | */ |
---|
92 | destroy: function() { |
---|
93 | // erase any drawn features |
---|
94 | this.erase(); |
---|
95 | |
---|
96 | this.map = null; |
---|
97 | |
---|
98 | this.events.destroy(); |
---|
99 | this.events = null; |
---|
100 | |
---|
101 | if (this.icon != null) { |
---|
102 | this.icon.destroy(); |
---|
103 | this.icon = null; |
---|
104 | } |
---|
105 | }, |
---|
106 | |
---|
107 | /** |
---|
108 | * Method: draw |
---|
109 | * Calls draw on the icon, and returns that output. |
---|
110 | * |
---|
111 | * Parameters: |
---|
112 | * px - {<OpenLayers.Pixel>} |
---|
113 | * |
---|
114 | * Returns: |
---|
115 | * {DOMElement} A new DOM Image with this marker's icon set at the |
---|
116 | * location passed-in |
---|
117 | */ |
---|
118 | draw: function(px) { |
---|
119 | return this.icon.draw(px); |
---|
120 | }, |
---|
121 | |
---|
122 | /** |
---|
123 | * Method: erase |
---|
124 | * Erases any drawn elements for this marker. |
---|
125 | */ |
---|
126 | erase: function() { |
---|
127 | if (this.icon != null) { |
---|
128 | this.icon.erase(); |
---|
129 | } |
---|
130 | }, |
---|
131 | |
---|
132 | /** |
---|
133 | * Method: moveTo |
---|
134 | * Move the marker to the new location. |
---|
135 | * |
---|
136 | * Parameters: |
---|
137 | * px - {<OpenLayers.Pixel>} the pixel position to move to |
---|
138 | */ |
---|
139 | moveTo: function (px) { |
---|
140 | if ((px != null) && (this.icon != null)) { |
---|
141 | this.icon.moveTo(px); |
---|
142 | } |
---|
143 | this.lonlat = this.map.getLonLatFromLayerPx(px); |
---|
144 | }, |
---|
145 | |
---|
146 | /** |
---|
147 | * APIMethod: isDrawn |
---|
148 | * |
---|
149 | * Returns: |
---|
150 | * {Boolean} Whether or not the marker is drawn. |
---|
151 | */ |
---|
152 | isDrawn: function() { |
---|
153 | var isDrawn = (this.icon && this.icon.isDrawn()); |
---|
154 | return isDrawn; |
---|
155 | }, |
---|
156 | |
---|
157 | /** |
---|
158 | * Method: onScreen |
---|
159 | * |
---|
160 | * Returns: |
---|
161 | * {Boolean} Whether or not the marker is currently visible on screen. |
---|
162 | */ |
---|
163 | onScreen:function() { |
---|
164 | |
---|
165 | var onScreen = false; |
---|
166 | if (this.map) { |
---|
167 | var screenBounds = this.map.getExtent(); |
---|
168 | onScreen = screenBounds.containsLonLat(this.lonlat); |
---|
169 | } |
---|
170 | return onScreen; |
---|
171 | }, |
---|
172 | |
---|
173 | /** |
---|
174 | * Method: inflate |
---|
175 | * Englarges the markers icon by the specified ratio. |
---|
176 | * |
---|
177 | * Parameters: |
---|
178 | * inflate - {float} the ratio to enlarge the marker by (passing 2 |
---|
179 | * will double the size). |
---|
180 | */ |
---|
181 | inflate: function(inflate) { |
---|
182 | if (this.icon) { |
---|
183 | var newSize = new OpenLayers.Size(this.icon.size.w * inflate, |
---|
184 | this.icon.size.h * inflate); |
---|
185 | this.icon.setSize(newSize); |
---|
186 | } |
---|
187 | }, |
---|
188 | |
---|
189 | /** |
---|
190 | * Method: setOpacity |
---|
191 | * Change the opacity of the marker by changin the opacity of |
---|
192 | * its icon |
---|
193 | * |
---|
194 | * Parameters: |
---|
195 | * opacity - {float} Specified as fraction (0.4, etc) |
---|
196 | */ |
---|
197 | setOpacity: function(opacity) { |
---|
198 | this.icon.setOpacity(opacity); |
---|
199 | }, |
---|
200 | |
---|
201 | /** |
---|
202 | * Method: setUrl |
---|
203 | * Change URL of the Icon Image. |
---|
204 | * |
---|
205 | * url - {String} |
---|
206 | */ |
---|
207 | setUrl: function(url) { |
---|
208 | this.icon.setUrl(url); |
---|
209 | }, |
---|
210 | |
---|
211 | /** |
---|
212 | * Method: display |
---|
213 | * Hide or show the icon |
---|
214 | * |
---|
215 | * display - {Boolean} |
---|
216 | */ |
---|
217 | display: function(display) { |
---|
218 | this.icon.display(display); |
---|
219 | }, |
---|
220 | |
---|
221 | CLASS_NAME: "OpenLayers.Marker" |
---|
222 | }); |
---|
223 | |
---|
224 | |
---|
225 | /** |
---|
226 | * Function: defaultIcon |
---|
227 | * Creates a default <OpenLayers.Icon>. |
---|
228 | * |
---|
229 | * Returns: |
---|
230 | * {<OpenLayers.Icon>} A default OpenLayers.Icon to use for a marker |
---|
231 | */ |
---|
232 | OpenLayers.Marker.defaultIcon = function() { |
---|
233 | var url = OpenLayers.Util.getImagesLocation() + "marker.png"; |
---|
234 | var size = new OpenLayers.Size(21, 25); |
---|
235 | var calculateOffset = function(size) { |
---|
236 | return new OpenLayers.Pixel(-(size.w/2), -size.h); |
---|
237 | }; |
---|
238 | |
---|
239 | return new OpenLayers.Icon(url, size, null, calculateOffset); |
---|
240 | }; |
---|
241 | |
---|
242 | |
---|