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 * @requires OpenLayers/Events.js
8 * @requires OpenLayers/Icon.js
12 * Class: OpenLayers.Marker
13 * Instances of OpenLayers.Marker are a combination of a
14 * <OpenLayers.LonLat> and an <OpenLayers.Icon>.
16 * Markers are generally added to a special layer called
17 * <OpenLayers.Layer.Markers>.
21 * var markers = new OpenLayers.Layer.Markers( "Markers" );
22 * map.addLayer(markers);
24 * var size = new OpenLayers.Size(10,17);
25 * var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
26 * var icon = new OpenLayers.Icon('http://boston.openguides.org/markers/AQUA.png',size,offset);
27 * markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0),icon));
28 * markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0),icon.clone()));
32 * Note that if you pass an icon into the Marker constructor, it will take
33 * that icon and use it. This means that you should not share icons between
34 * markers -- you use them once, but you should clone() for any additional
35 * markers using that same icon.
37 OpenLayers.Marker = OpenLayers.Class({
41 * {<OpenLayers.Icon>} The icon used by this marker.
47 * {<OpenLayers.LonLat>} location of object
53 * {<OpenLayers.Events>} the event handler.
59 * {<OpenLayers.Map>} the map this marker is attached to
64 * Constructor: OpenLayers.Marker
66 * lonlat - {<OpenLayers.LonLat>} the position of this marker
67 * icon - {<OpenLayers.Icon>} the icon for this marker
69 initialize: function(lonlat, icon) {
72 var newIcon = (icon) ? icon : OpenLayers.Marker.defaultIcon();
73 if (this.icon == null) {
76 this.icon.url = newIcon.url;
77 this.icon.size = newIcon.size;
78 this.icon.offset = newIcon.offset;
79 this.icon.calculateOffset = newIcon.calculateOffset;
81 this.events = new OpenLayers.Events(this, this.icon.imageDiv, null);
86 * Destroy the marker. You must first remove the marker from any
87 * layer which it has been added to, or you will get buggy behavior.
88 * (This can not be done within the marker since the marker does not
89 * know which layer it is attached to.)
92 // erase any drawn features
97 this.events.destroy();
100 if (this.icon != null) {
108 * Calls draw on the icon, and returns that output.
111 * px - {<OpenLayers.Pixel>}
114 * {DOMElement} A new DOM Image with this marker's icon set at the
118 return this.icon.draw(px);
123 * Erases any drawn elements for this marker.
126 if (this.icon != null) {
133 * Move the marker to the new location.
136 * px - {<OpenLayers.Pixel>} the pixel position to move to
138 moveTo: function (px) {
139 if ((px != null) && (this.icon != null)) {
140 this.icon.moveTo(px);
142 this.lonlat = this.map.getLonLatFromLayerPx(px);
149 * {Boolean} Whether or not the marker is drawn.
151 isDrawn: function() {
152 var isDrawn = (this.icon && this.icon.isDrawn());
160 * {Boolean} Whether or not the marker is currently visible on screen.
162 onScreen:function() {
164 var onScreen = false;
166 var screenBounds = this.map.getExtent();
167 onScreen = screenBounds.containsLonLat(this.lonlat);
174 * Englarges the markers icon by the specified ratio.
177 * inflate - {float} the ratio to enlarge the marker by (passing 2
178 * will double the size).
180 inflate: function(inflate) {
182 var newSize = new OpenLayers.Size(this.icon.size.w * inflate,
183 this.icon.size.h * inflate);
184 this.icon.setSize(newSize);
190 * Change the opacity of the marker by changin the opacity of
194 * opacity - {float} Specified as fraction (0.4, etc)
196 setOpacity: function(opacity) {
197 this.icon.setOpacity(opacity);
202 * Change URL of the Icon Image.
206 setUrl: function(url) {
207 this.icon.setUrl(url);
212 * Hide or show the icon
214 * display - {Boolean}
216 display: function(display) {
217 this.icon.display(display);
220 CLASS_NAME: "OpenLayers.Marker"
225 * Function: defaultIcon
226 * Creates a default <OpenLayers.Icon>.
229 * {<OpenLayers.Icon>} A default OpenLayers.Icon to use for a marker
231 OpenLayers.Marker.defaultIcon = function() {
232 var url = OpenLayers.Util.getImagesLocation() + "marker.png";
233 var size = new OpenLayers.Size(21, 25);
234 var calculateOffset = function(size) {
235 return new OpenLayers.Pixel(-(size.w/2), -size.h);
238 return new OpenLayers.Icon(url, size, null, calculateOffset);